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