​LeetCode刷题实战116:填充每个节点的下一个右侧节点指针

算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从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;
    }
}

好了,今天的文章就到这里,如果觉得有所收获,请顺手点个在看或者转发吧,你们的支持是我最大的动力。

上期推文:

LeetCode1-100题汇总,希望对你有点帮助!

LeetCode刷题实战101:对称二叉树

LeetCode刷题实战102:二叉树的层序遍历

LeetCode刷题实战103:二叉树的锯齿形层次遍历

LeetCode刷题实战104:二叉树的最大深度

LeetCode刷题实战105:从前序与中序遍历序列构造二叉树

LeetCode刷题实战106:从中序与后序遍历序列构造二叉树

LeetCode刷题实战107:二叉树的层次遍历 II

LeetCode刷题实战108:将有序数组转换为二叉搜索树

LeetCode刷题实战109:有序链表转换二叉搜索树

LeetCode刷题实战110:平衡二叉树

LeetCode刷题实战111:二叉树的最小深度

LeetCode刷题实战112:路径总和

LeetCode刷题实战113:路径总和 II

LeetCode刷题实战114:二叉树展开为链表

LeetCode刷题实战115:不同的子序列

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值