LeetCode:Populating Next Right Pointers in Each Node I & II

Populating Next Right Pointers in Each Node

Given a binary tree

    struct TreeLinkNode {
      TreeLinkNode *left;
      TreeLinkNode *right;
      TreeLinkNode *next;
    }

Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.

Initially, all next pointers are set to NULL.

Note:

  • You may only use constant extra space.
  • You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).

For example,
Given the following perfect binary tree,

         1
       /  \
      2    3
     / \  / \
    4  5  6  7

After calling your function, the tree should look like:

         1 -> NULL
       /  \
      2 -> 3 -> NULL
     / \  / \
    4->5->6->7 -> NULL

直接想法就是利用二叉樹的層次遍歷,但是需要建一個隊列,空間複雜度不滿足要求,之後繼續想,由於是完全二叉樹,除葉子節點外每個節點都有左右子數,非葉子節點p的左子樹的next是p的右子樹,p的右子樹的next是p的next的左子樹,這樣的思路下可以遞歸構建該樹:
/**
 * Definition for binary tree with next pointer.
 * public class TreeLinkNode {
 *     int val;
 *     TreeLinkNode left, right, next;
 *     TreeLinkNode(int x) { val = x; }
 * }
 */
public class Solution {
    int count=0,n=1;
    public void connect(TreeLinkNode root) {
        if(root==null)return ;
        if(root.left==null)return ;
        TreeLinkNode p,q;
        p=root;
        while(p!=null)
        {//處理同一行的節點
            if(p.next!=null){q=p.next.left;}
            else q=null;
            p.left.next=p.right;
            p.right.next=q;
            p=p.next;
        }
        connect(root.left);
        connect(root.right);
    }
}

Populating Next Right Pointers in Each Node II

Follow up for problem "Populating Next Right Pointers in Each Node".

What if the given tree could be any binary tree? Would your previous solution still work?

Note:

  • You may only use constant extra space.

For example,
Given the following binary tree,

         1
       /  \
      2    3
     / \    \
    4   5    7

After calling your function, the tree should look like:

         1 -> NULL
       /  \
      2 -> 3 -> NULL
     / \    \
    4-> 5 -> 7 -> NULL

與I的不同是完全二叉樹變成了隨意的二叉樹,如果沒有空間複雜度的要求的話依舊可以用層次遍歷的方法,設置一個flag節點隔開每一行,隊列中每節點的next等於隊列中的下一個節點(該節點不是flag),空間複雜度比較大,但是理解起來比較直觀,依然能夠通過OJ:
/**
 * 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 ;
        root.next=null;
        TreeLinkNode p=root,q,flag=new TreeLinkNode(0);
        flag.left=flag;
        Queue<TreeLinkNode> queue=new LinkedList<TreeLinkNode>();
        if(root.left!=null)queue.add(root.left);
        if(root.right!=null)queue.add(root.right);
        if(queue.isEmpty())return ;
        queue.add(flag);
        while(!queue.isEmpty())
        {
            p=queue.poll();
        
            if(p.left==p)
            {
                if(queue.isEmpty())return ;
                queue.add(flag);
                continue;
            }
            q=queue.peek();
            if(q.left==q)p.next=null;
            else p.next=q;
        if(p.left!=null)queue.add(p.left);
        if(p.right!=null)queue.add(p.right);
        }
    }
}
下面想到一種空間複雜度比較小的算法,在不完全二叉樹中,一個節點P的子節點的next(如果p左右子樹都有的話p.left.next=p.right)是p的同層級的節點中最”靠近“p的非葉子節點的節點q的一個子樹,因此對p的next進行遞歸遍歷,尋找p的子節點的next,按照這樣的思想建立新樹:
/**
 * 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;
        if(root.left==null && root.right==null)return ;
        TreeLinkNode p=root,q;
        while(p!=null)
        {
            if(p.left!=null)
            {
                if(p.right!=null)p.left.next=p.right;
                else 
                    {
                        q=p.next;
                        while(q!=null)
                        {   
                            if(q.left!=null){p.left.next=q.left;break;}
                            if(q.right!=null){p.left.next=q.right;break;}
                            q=q.next;
                        }
                
                    }
            }// p.left!=null
            if(p.right!=null)
            {
                q=p.next;
                        while(q!=null)
                        {   
                            if(q.left!=null){p.right.next=q.left;break;}
                            if(q.right!=null){p.right.next=q.right;break;}
                            q=q.next;
                        }
                
            }
            p=p.next;
        }
        connect(root.left);
        connect(root.right);
    }
}




 
發佈了34 篇原創文章 · 獲贊 0 · 訪問量 9655
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章