您的位置:首页 > Web前端 > Node.js

Populating Next Right Pointers in Each Node

2017-01-02 20:08 274 查看

1.题目

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
.

2.算法



public void connect(TreeLinkNode root) {
if(root == null)
return;
TreeLinkNode lastHead = root;
TreeLinkNode current = null;
TreeLinkNode curHead = null;
while(lastHead!=null)
{
TreeLinkNode lastCur = lastHead;
while(lastCur != null)
{
if(lastCur.left!=null)
{
if(curHead == null)
{
curHead = lastCur.left;
current = curHead;
}
else
{
current.next = lastCur.left;
current = current.next;
}
}
if(lastCur.right!=null)
{
if(curHead == null)
{
curHead = lastCur.right;
current = curHead;
}
else
{
current.next = lastCur.right;
current = current.next;
}
}
lastCur = lastCur.next;

}
lastHead = curHead;
curHead = null;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: