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 toNULL.
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;
}
}
相关文章推荐
- Leetcode Populating Next Right Pointers in Each Node
- LeetCode 116 Populating Next Right Pointers in Each Node
- LeetCode: Populating Next Right Pointers in Each Node
- [LeetCode] Populating Next Right Pointers in Each Node
- Leetcode: Populating Next Right Pointers in Each Node
- leetcode Populating Next Right Pointers in Each Node II
- [leetcode]Populating Next Right Pointers in Each Node
- [Leetcode]Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node II
- 【LeetCode】Populating Next Right Pointers in Each Node
- LeetCode: Populating Next Right Pointers in Each Node II [117]
- 按层空间复杂度Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node 更好的方法待补充
- leetcode--populating_next_right_pointers_in_each_node
- leetcode - Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- LeetCode(Oct28'12):Populating Next Right Pointers in Each Node II
- 【leetcode】Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node