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

[LeetCode] Populating Next Right Pointers in Each Node

 1 public class Solution { 2     public void connect(TreeLinkNode root) { 3         if (root==null) return; 4         TreeLinkNode upperLevelStartNode = root; 5          6         while(upperLevelStartNode.left!=null) { 7             TreeLinkNode travelingNode=upperLevelStartNode; 8             while (true) { 9                 travelingNode.left.next=travelingNode.right;10                 if (travelingNode.next == null) break;11                 travelingNode.right.next = travelingNode.next.left;12                 travelingNode = travelingNode.next;13             } 14             upperLevelStartNode = upperLevelStartNode.left;15         }16     }17 }

 

[LeetCode] Populating Next Right Pointers in Each Node