/**
* Definition for binary tree with next pointer.
* public class TreeLinkNode {
* int val;
* TreeLinkNode left, right, next;
* TreeLinkNode(int x) { val = x; }
* }
*/
public class Solution {
public void connect(TreeLinkNode root) {
if(root == null){
return;
}
TreeLinkNode first = null;
TreeLinkNode second = null;
TreeLinkNode pre = root;
root.next = null;
while(pre.left != null){
root = pre;
pre = pre.left;
while(true){
if(root.next == null){
first = root.left;
second = root.right;
first.next = second;
second.next = null;
break;
}
else{
first = root.left;
second = root.right;
first.next = second;
first = second;
root = root.next;
second = root.left;
first.next = second;
}
}
}
}
}
Populating Next Right Pointers in Each Node
最新推荐文章于 2022-02-28 18:16:11 发布