力扣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;
    }
}
相关推荐
一叶飘零_sweeeet15 分钟前
不止于 API 调用:解锁 Java 工具类设计的三重境界 —— 可复用性、线程安全与性能优化
java·工具类
cynicme1 小时前
力扣3228——将 1 移动到末尾的最大操作次数
算法·leetcode
熬了夜的程序员1 小时前
【LeetCode】109. 有序链表转换二叉搜索树
数据结构·算法·leetcode·链表·职场和发展·深度优先
随意起个昵称1 小时前
【递归】二进制字符串中的第K位
c++·算法
mjhcsp2 小时前
C++ 循环结构:控制程序重复执行的核心机制
开发语言·c++·算法
立志成为大牛的小牛2 小时前
数据结构——四十一、分块查找(索引顺序查找)(王道408)
数据结构·学习·程序人生·考研·算法
A阳俊yi2 小时前
Spring Data JPA
java·开发语言
小王不爱笑1322 小时前
Spring AOP(AOP+JDBC 模板 + 转账案例)
java·后端·spring
遇印记2 小时前
蓝桥java蜗牛
java·学习·蓝桥杯
xier_ran2 小时前
深度学习:RMSprop 优化算法详解
人工智能·深度学习·算法