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




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值