116. Populating Next Right Pointers in Each Node

题目描述(中等难度)

在这里插入图片描述
给定一个满二叉树,每个节点多了一个next指针,然后将所有的next指针指向它的右边的节点。并且要求空间复杂度是O(1)

解法一 BFS

如果没有要求空间复杂度这道题就简单多了,我们只需要用一个队列做BFS。然后按顺序将每个节点连起来就可以了。

import java.util.LinkedList;
import java.util.Queue;

class Node{
	int val;
	Node left;
	Node right;
	Node next;
	Node(int x){val=x;}
}

public class Populating_Next_Right_Pointers_in_Each_Node {

	public static Node connect(Node root) {
		
		if(root==null) return root;
		
		Queue<Node>queue=new LinkedList<Node>();
		queue.offer(root);
		while(!queue.isEmpty()) {
			int size=queue.size();
			Node pre=null;
			for(int i=0;i<size;i++) {
				Node cur=queue.poll();
				
				pre=cur;
				if(cur.left!=null) {
					queue.offer(cur.left);
				}
				if(cur.right!=null) {
					queue.offer(cur.right);
				}
			}
			
		}
		return root;
		
	}
	
	public static void main(String args[]) {
		Node[] node=new Node[7];
		node[0]=new Node(1);
		node[1]=new Node(2);
		node[2]=new Node(0);
		node[3]=new Node(4);
		node[4]=new Node(5);
		node[5]=new Node(6);
		node[6]=new Node(7);
		
		node[0].left=node[1];
		node[0].right=node[2];
		node[1].left=node[3];
		node[1].right=node[4];
		node[2].left=node[5];
		node[2].right=node[6];
		
		Node ans=connect(node[0]);
		System.out.println(ans);
	}
}
}
解法二 迭代

当然既然题目要求了空间复杂度,那么我们来考虑下不用队列该怎么处理。只需要解决三个问题就够了。

  • 每一层怎么遍历?

之前是用队列将下一层的节点保存了起来。

这里的话,其实只需要提前把下一层的next构造完成,到了下一层的时候就可以遍历了。

  • 什么时候进入下一层?

之前是得到当前队列的元素个数,然后遍历那么多次。

这里的话,注意到最右边的节点的next为null,所以可以判断当前遍历的节点是不是null。

  • 怎么得到每层开头节点?

之前队列把当前层的所以节点存了起来,得到开头节点当然很容易。

这里的话,我们额外需要一个变量把它存起来。

三个问题都解决了,就可以写代码了。利用三个指针,start 指向每层的开始节点,cur指向当前遍历的节点,pre指向当前遍历的节点的前一个节点。

在这里插入图片描述

如上图,我们需要把 pre 的左孩子的 next 指向右孩子,pre 的右孩子的next指向cur的左孩子。
在这里插入图片描述
如上图,当 cur 指向 null 以后,我们只需要把 pre 的左孩子的 next 指向右孩子。

public Node connect(Node root) {
    if (root == null) {
        return root;
    }
    Node pre = root;
    Node cur = null;
    Node start = pre;
    while (pre.left != null) {
        //遍历到了最右边的节点,要将 pre 和 cur 更新到下一层,并且用 start 记录
        if (cur == null) {
            //我们只需要把 pre 的左孩子的 next 指向右孩子。
            pre.left.next = pre.right;

            pre = start.left;
            cur = start.right;
            start = pre;
        //将下一层的 next 连起来,同时 pre、next 后移
        } else {
            //把 pre 的左孩子的 next 指向右孩子
            pre.left.next = pre.right;
            //pre 的右孩子的 next 指向 cur 的左孩子。
            pre.right.next = cur.left;

            pre = pre.next;
            cur = cur.next;
        }
    }
    return root;
}
总结

题目让我们初始化 next 指针,初始化过程中我们又利用到了next指针,很巧妙了。

参考文献

1.https://zhuanlan.zhihu.com/p/76163759
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安替-AnTi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值