力扣labuladong——一刷day63

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • [一、力扣1080. 根到叶路径上的不足节点](#一、力扣1080. 根到叶路径上的不足节点)
  • [二、力扣117. 填充每个节点的下一个右侧节点指针 II](#二、力扣117. 填充每个节点的下一个右侧节点指针 II)
  • [三、力扣662. 二叉树最大宽度](#三、力扣662. 二叉树最大宽度)

前言


二叉树大部分题目都可以用递归的算法解决,但少部分题目用递归比较麻烦的话,我们可以考虑使用层序遍历的方式解决。

一、力扣1080. 根到叶路径上的不足节点

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode sufficientSubset(TreeNode root, int limit) {
        if(root == null){
            return null;
        }
        if(root.left == null && root.right == null){
            if(root.val < limit){
                return null;
            }
            return root;
        }
        TreeNode left = sufficientSubset(root.left,limit - root.val);
        TreeNode right = sufficientSubset(root.right, limit - root.val);
        if(left == null && right == null){
            return null;
        }
        root.left = left;
        root.right = right;
        return root;
    }
}

二、力扣117. 填充每个节点的下一个右侧节点指针 II

java 复制代码
/*
// Definition for a Node.
class Node {
    public int val;
    public Node left;
    public Node right;
    public Node next;

    public Node() {}
    
    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, Node _left, Node _right, Node _next) {
        val = _val;
        left = _left;
        right = _right;
        next = _next;
    }
};
*/

class Solution {
    public Node connect(Node root) {
        if(root == null){
            return null;
        }
        Deque<Node> deq = new ArrayDeque<>();
        deq.offerLast(root);
        while(!deq.isEmpty()){
            int size = deq.size();
            Node pre = deq.peekFirst();
            for(int i = 0; i < size; i ++){
                Node cur = deq.pollFirst();
                if(cur.left != null){
                    deq.offerLast(cur.left);
                }
                if(cur.right != null){
                    deq.offerLast(cur.right);
                }
                if(i >0){
                    pre.next = cur;
                    pre = cur;
                }
            }
        }
        return root;
    }
}

三、力扣662. 二叉树最大宽度

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    class Pair{
        TreeNode node;
        int id;
        public Pair(TreeNode node, int id){
            this.node = node;
            this.id = id;
        }
    }
    public int widthOfBinaryTree(TreeNode root) {
        Deque<Pair> deq = new ArrayDeque<>();
        deq.offerLast(new Pair(root,1));
        int res = 1;
        while(!deq.isEmpty()){
            int size = deq.size();
            int low = 0, high = 0;
            for(int i = 0; i < size; i ++){
                Pair cur = deq.pollFirst();
                TreeNode curNode = cur.node;
                int curId = cur.id;
                if(curNode.left != null){
                    deq.offerLast(new Pair(curNode.left,curId*2));
                }
                if(curNode.right != null){
                    deq.offerLast(new Pair(curNode.right,curId*2+1));
                }
                if(i == 0){
                    low = curId;
                }
                if(i == size-1){
                    high = curId;
                }
            }
            res = Math.max(res, high-low+1);
        }
        return res;
    }
}
相关推荐
代码雕刻家22 分钟前
数据结构-3.9.栈在递归中的应用
c语言·数据结构·算法
雨中rain22 分钟前
算法 | 位运算(哈希思想)
算法
2402_8575893626 分钟前
“衣依”服装销售平台:Spring Boot框架的设计与实现
java·spring boot·后端
吾爱星辰1 小时前
Kotlin 处理字符串和正则表达式(二十一)
java·开发语言·jvm·正则表达式·kotlin
哎呦没2 小时前
大学生就业招聘:Spring Boot系统的架构分析
java·spring boot·后端
Kalika0-02 小时前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法
编程、小哥哥2 小时前
netty之Netty与SpringBoot整合
java·spring boot·spring
代码雕刻家2 小时前
课设实验-数据结构-单链表-文教文化用品品牌
c语言·开发语言·数据结构
sp_fyf_20242 小时前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-02
人工智能·神经网络·算法·计算机视觉·语言模型·自然语言处理·数据挖掘
IT学长编程3 小时前
计算机毕业设计 玩具租赁系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·玩具租赁系统