116. Populating Next Right Pointers in Each Node

You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:

struct Node {
  int val;
  Node *left;
  Node *right;
  Node *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.

 

Follow up:

  • You may only use constant extra space.
  • Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem.

 

Example 1:

 

Input: root = [1,2,3,4,5,6,7]
Output: [1,#,2,3,#,4,5,6,7,#]
Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.

 

Constraints:

  • The number of nodes in the given tree is less than 4096.
  • -1000 <= node.val <= 1000

 

来自 <https://leetcode.com/problems/populating-next-right-pointers-in-each-node/>

 

 

题解:给一棵完全二叉树,叶节点都在同一层,并且每个父亲节点都有两个叶节点。节点的数据结构中有一个next变量,它指向的是同一层的右边的节点,初始时每个节点的next的值都是null,为树中的叶节点赋值上合理的next值。

 

首先呢,看到这个题目,第一个想法是使用层级遍历的方法,因为图中很明显的是在同一层中进行操作。

将每一层的节点加载到队列中后,按照顺序为每个节点赋值next,比如,队列中第一个节点的next值就是队列中的第二个节点。将一层中的节点都赋值完成后,在将这一层的节点从队列中剔除,并且将他们的子节点加载到队列中。

 

 

    public Node connect(Node root) {

        List<Node> queue = new LinkedList<>();

        if(root == null || root.left == null) return root;

        queue.add(root.left);

        queue.add(root.right);

        while(!queue.isEmpty()){

            int len = queue.size();

            for (int i = 0; i < queue.size()-1; i++) {

                queue.get(i).next = queue.get(i+1);

            }

            for(int i = 0; i < len; i++){

                Node temp = queue.remove(0);

                if(temp.left != null)

                    queue.add(temp.left);

                if(temp.right != null)

                    queue.add(temp.right);

            }

        }

        return root;

    }

 

 

然后,另一种时间复杂度低的方法是,使用标记变量。

    public Node connect(Node root) {

        if(root == null || root.left == null) return root;

        root.left.next = root.right;

        //dep 用来标记每层的第一个节点

        Node dep = root.left;

        if(dep.left == null) return root;

        //currentNode 用来标记当前的节点

        Node currentNode = dep.left;

        while(dep != null){

            //temp当前节点的父节点

            Node temp = dep;

            currentNode.next = temp.right;

            currentNode = currentNode.next;

            temp = temp.next;

            while(temp != null){

                if(temp.left != null){

                    currentNode.next = temp.left;

                    currentNode = currentNode.next;

                }

                if(temp.right != null){

                    currentNode.next = temp.right;

                    currentNode = currentNode.next;

                }

                temp = temp.next;

            }

            dep = dep.left;

            currentNode = dep;

            if(currentNode == null) break;

        }

        return root;

    }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值