Problem 116: Populating Next Right Pointers in Each Node
思路
/**
* 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) {
while (root != null) {
TreeLinkNode node = root;
while (node != null && node.left != null) {
node.left.next = node.right;
node.right.next = (node.next == null ? null : node.next.left);
node = node.next;
}
root = root.left;
}
}
}PreviousProblem 108: Convert Sorted Array to Binary Search TreeNextProblem 117: Populating Next Right Pointers in Each Node II
Last updated