117. Populating Next Right Pointers in Each Node II

时间:2019-03-15
本文章向大家介绍117. Populating Next Right Pointers in Each Node II,主要包括117. Populating Next Right Pointers in Each Node II使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

 

//按层从左往右检索
public class Solution {
    public void connect(TreeLinkNode root) {
        
        while (root != null) {
            TreeLinkNode tempChild = new TreeLinkNode(0);
            TreeLinkNode currentChild = tempChild;
            while (root != null) {
                if (root.left != null) {
                    currentChild.next = root.left;
                    currentChild = currentChild.next;
                }
                if (root.right != null) {
                    currentChild.next = root.right;
                    currentChild = currentChild.next;
                }
                root = root.next;    
            }
            root = tempChild.next;
        }
    }
}