117. Populating Next Right Pointers in Each Node II

和上一个一样,要在当前层建好下一层的next linked list,便于下次遍历。

用通式写的,感觉是背过答案了。。。

public class Solution {
    public void connect(TreeLinkNode root) {
        while (root != null) {
            TreeLinkNode head = new TreeLinkNode(0);
            TreeLinkNode temp = head;
            for( ; root != null; root = root.next) {
                
                if (root.left != null) {
                    temp.next = root.left;
                    temp = temp.next;
                }
                
                if (root.right != null) {
                    temp.next = root.right;
                    temp = temp.next;
                }
            }
            root = head.next;
        }
    }
}
原文地址:https://www.cnblogs.com/reboot329/p/6107425.html