LeetCode刷题笔录 Populating Next Right Pointers in Each Node

50 篇文章 0 订阅

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

这题的想法就是level order traversal。用一个queue来存储node,另外一个queue来存储每个node所在的层数。

每次从nodeQueue中取出一个node,levelQueue中取出其层数。当:

1.nodeQueue没有下一个node

2.nodeQueue有下一个node,但下一个node的层数和现在的node的层数不一样

node.next=null。

剩下的情况就是node.next=nodeQueue中下一个元素。

<pre name="code" class="java">public class Solution {
    public void connect(TreeLinkNode root) {
        if(root == null)
            return;
        LinkedList<TreeLinkNode> nodeQueue = new LinkedList<TreeLinkNode>();
        LinkedList<Integer> levelQueue = new LinkedList<Integer>();
        nodeQueue.addLast(root);
        levelQueue.add(0);
        while(!nodeQueue.isEmpty()){
            TreeLinkNode node = nodeQueue.pollFirst();
            int level = levelQueue.pollFirst();
            //if this is level 0
            //or if there is no more node in the queue 
            //or the next node's level is not equal to the current node's level
            //then node.next is null
            if(level == 0){
                node.next = null;
            }
            else if(nodeQueue.isEmpty()){
                node.next = null;
            }
            else{
                if(level != levelQueue.peekFirst())
                    node.next = null;
            //otherwise, node.next equals the next node in the queue
                else{
                    node.next = nodeQueue.peekFirst();
                }
            }
            if(node.left != null){
                nodeQueue.addLast(node.left);
                levelQueue.addLast(level + 1);
            }
            if(node.right != null){
                nodeQueue.addLast(node.right);
                levelQueue.addLast(level + 1);
            }
        }
    }
}
 

更新:

回头看这个问题发现要求有constant extra space,于是不能用queue了。不过还好这里每个node都有一个next pointer,可以利用next来进行层序遍历。

要点就是要记录下一层的起点node,以及前一个node的right child。

public class Solution {
    public void connect(TreeLinkNode root) {
        if(root == null)
            return;
        //this pointer holds the head of the next level
        TreeLinkNode nextLineFirst = root;
        
        //outer loop loops between levels
        while(nextLineFirst != null){
            TreeLinkNode current = nextLineFirst;
            TreeLinkNode previousRight = null;
            nextLineFirst = null;
            
            //inner loop loops within in a level, between nodes
            while(current != null){
                if(nextLineFirst == null && current.left != null){
                    //update the next level head pointer
                    nextLineFirst = current.left;
                    
                }
                
                if(current.left != null){
                    //current is not a leaf, link its left child to its right child
                    current.left.next = current.right;
                }
                if(previousRight != null){
                    //link the previous right child to the current node's left child
                    previousRight.next = current.left;
                }
                
                previousRight = current.right;
                current = current.next;
            }
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值