力扣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;
    }
}
相关推荐
Seven9712 分钟前
【Guava】集合工具Collections2
java
MaCa .BaKa13 分钟前
25-智慧旅游系统(协同算法)三端
java·javascript·vue.js·spring boot·tomcat·maven·旅游
君义_noip17 分钟前
信息学奥赛一本通 1524:旅游航道
c++·算法·图论·信息学奥赛
西元.18 分钟前
线程等待与唤醒的几种方法与注意事项
java·开发语言
栗筝i21 分钟前
Spring 核心技术解析【纯干货版】- XVI:Spring 网络模块 Spring-WebMvc 模块精讲
java·网络·spring
烁34725 分钟前
每日一题(小白)动态规划篇5
算法·动态规划
独好紫罗兰26 分钟前
洛谷题单2-P5717 【深基3.习8】三角形分类-python-流程图重构
开发语言·python·算法
落榜程序员27 分钟前
Java基础-25-继承-方法重写-子类构造器的特点-构造器this的调用
java·开发语言
Debug 熊猫29 分钟前
【Java基础】10章、单例模式、final关键字的使用技巧和使用细节、单例模式-懒汉式、单例模式-饿汉式【3】
java·javascript·后端·单例模式
shaoweijava29 分钟前
基于SpringBoot的求职招聘网站系统(源码+数据库)
java·spring boot·mysql·spring