算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
今天和大家聊的问题叫做 填充每个节点的下一个右侧节点指针,我们先来看题面:https://leetcode-cn.com/problems/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.
题意
给定一个完美二叉树,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下:
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。
初始状态下,所有 next 指针都被设置为 NULL。
样例
解题
https://zhuanlan.zhihu.com/p/132431048
树和图的两种基本遍历方法。一种是深度优先方法,例如:每次只遍历一个分支;另外一种是广度优先方法,例如:先遍历完这一层再进入下一层。树的深度优先遍历又可以分为先序遍历 preorder
、中序遍历 inorder
和后序遍历postorder
。树的广度优先遍历基于节点的层级 level
概念。一个节点的层级取决于该节点的深度或者到根节点的距离。需要先遍历完同一层级的所有节点,才能进入下一层级。
很明显,此问题应该使用广度优先遍历解决。使用广度优先遍历,可以将同一层级的所有节点连接起来。
class Solution {
public Node connect(Node root) {
if (root == null) {
return root;
}
// Initialize a queue data structure which contains
// just the root of the tree
Queue<Node> Q = new LinkedList<Node>();
Q.add(root);
// Outer while loop which iterates over
// each level
while (Q.size() > 0) {
// Note the size of the queue
int size = Q.size();
// Iterate over all the nodes on the current level
for(int i = 0; i < size; i++) {
// Pop a node from the front of the queue
Node node = Q.poll();
// This check is important. We don't want to
// establish any wrong connections. The queue will
// contain nodes from 2 levels at most at any
// point in time. This check ensures we only
// don't establish next pointers beyond the end
// of a level
if (i < size - 1) {
node.next = Q.peek();
}
// Add the children, if any, to the back of
// the queue
if (node.left != null) {
Q.add(node.left);
}
if (node.right != null) {
Q.add(node.right);
}
}
}
// Since the tree has now been modified, return the root node
return root;
}
}
好了,今天的文章就到这里,如果觉得有所收获,请顺手点个在看或者转发吧,你们的支持是我最大的动力。
上期推文:
LeetCode刷题实战105:从前序与中序遍历序列构造二叉树