力扣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;
    }
}
相关推荐
木子.李3476 分钟前
数据结构-算法C++(额外问题汇总)
数据结构·c++·算法
板板正23 分钟前
EasyExcel实现普通导入导出以及按模板导出excel文件
java·excel
花心蝴蝶.28 分钟前
API签名认证算法全解析
算法
野犬寒鸦30 分钟前
从零起步学习Redis || 第十章:主从复制的实现流程与常见问题处理方案深层解析
java·服务器·数据库·redis·后端·缓存
luopandeng32 分钟前
amd npt技术 对比 intel ept 技术
java·linux·网络
兮山与34 分钟前
算法6.0
算法
编啊编程啊程34 分钟前
兼职管理平台
java·spring boot·rpc·dubbo·nio
代码对我眨眼睛35 分钟前
739. 每日温度 LeetCode 热题 HOT 100
算法·leetcode
吃饭最爱1 小时前
java项目中前后端结合的要点
java
程序员莫小特1 小时前
老题新解|计算2的N次方
开发语言·数据结构·算法·青少年编程·信息学奥赛一本通