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;
        }
    }
}

 

posted @ 2019-03-15 12:02  MarkLeeBYR  阅读(67)  评论(0)    收藏  举报