首页 > 代码库 > [LeetCode] Populating Next Right Pointers in Each Node II

[LeetCode] Populating Next Right Pointers in Each Node II

 1 public class Solution { 2     public void connect(TreeLinkNode root) { 3         TreeLinkNode upperTravelingNode = root; 4         if (root==null) return; 5         TreeLinkNode nowLevelStartNode = null; 6         TreeLinkNode nowLevelTravelingNode = null; 7          8         do { 9             while (upperTravelingNode != null) {10                 if (upperTravelingNode.left!=null) {11                     if (nowLevelStartNode==null) {12                         nowLevelStartNode=upperTravelingNode.left;13                     } else {14                         nowLevelTravelingNode.next = upperTravelingNode.left;15                     }16                     nowLevelTravelingNode = upperTravelingNode.left;17                 }18                 if (upperTravelingNode.right!=null) {19                     if (nowLevelStartNode==null) {20                         nowLevelStartNode=upperTravelingNode.right;21                     } else {22                         nowLevelTravelingNode.next = upperTravelingNode.right;23                     }24                     nowLevelTravelingNode = upperTravelingNode.right;                    25                 }26                 upperTravelingNode = upperTravelingNode.next;27             }28             upperTravelingNode = nowLevelStartNode;29             nowLevelStartNode = null;30         } while (upperTravelingNode != null);31     }32 }

 

[LeetCode] Populating Next Right Pointers in Each Node II