Java基础算法每日5道详解(4)

news2025/4/7 2:58:48

101. Symmetric Tree

对称树

Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).

Example 1:

https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg

Input: root = [1,2,2,3,4,4,3]
Output: true

Example 2:

https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg

Input: root = [1,2,2,null,3,null,3]
Output: false

leetcode代码

/**
 * 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 boolean isSymmetric(TreeNode root) {
        if (root == null) return true;
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root.left);
        stack.push(root.right);
        while (!stack.empty()) {
            TreeNode n1 = stack.pop(), n2 = stack.pop();
            if (n1 == null && n2 == null) continue;
            if (n1 == null || n2 == null || n1.val != n2.val) return false;
            stack.push(n1.left);
            stack.push(n2.right);
            stack.push(n1.right);
            stack.push(n2.left);
        }
        return true;
    }
}

104. Maximum Depth of Binary Tree

二叉树的最大深度

Given the root of a binary tree, return its maximum depth.

A binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Example 1:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ptCPzawy-1670866158609)(https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg)]

Input: root = [3,9,20,null,null,15,7]
Output: 3

Example 2:

Input: root = [1,null,2]
Output: 2

leetcode代码

/**
 * 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 int maxDepth(TreeNode root) {
        if(root == null) return 0;
        
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        return Math.max(left, right) + 1;        
    }
}

108. Convert Sorted Array to Binary Search Tree

将排序数组转换为二叉搜索树

Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree.

A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one.

Example 1:

https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg

Input: nums = [-10,-3,0,5,9]
Output: [0,-3,9,-10,null,5]
Explanation: [0,-10,5,null,-3,null,9] is also accepted:

https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg

Example 2:

https://assets.leetcode.com/uploads/2021/02/18/btree.jpg

Input: nums = [1,3]
Output: [3,1]
Explanation: [1,null,3] and [3,1] are both height-balanced BSTs.

leetcode代码

/**
 * 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 sortedArrayToBST(int[] num) {
    if (num.length == 0) {
        return null;
    }
    TreeNode head = helper(num, 0, num.length - 1);
    return head;
    }

    public TreeNode helper(int[] num, int low, int high) {
        if (low > high) { // Done
            return null;
        }
        int mid = (low + high) / 2;
        TreeNode node = new TreeNode(num[mid]);
        node.left = helper(num, low, mid - 1);
        node.right = helper(num, mid + 1, high);
        return node;
    }
}

110. Balanced Binary Tree

平衡二叉树

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as:

a binary tree in which the left and right subtrees of every node differ in height by no more than 1.

Example 1:

https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg

Input: root = [3,9,20,null,null,15,7]
Output: true

Example 2:

https://assets.leetcode.com/uploads/2020/10/06/balance_2.jpg

Input: root = [1,2,2,3,3,null,null,4,4]
Output: false

Example 3:

Input: root = []
Output: true

leetcode代码

/**
 * 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 boolean isBalanced(TreeNode root) {
        if(root==null) return true;
        Stack<TreeNode> stack = new Stack<TreeNode>();
        Map<TreeNode, Integer> map = new HashMap<TreeNode, Integer>();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode node = stack.pop();
            if((node.left==null || node.left!=null && map.containsKey(node.left)) &&(node.right==null || node.right!=null && map.containsKey(node.right))){
                int left = node.left==null?0:map.get(node.left);
                int right = node.right==null?0:map.get(node.right);
                if(Math.abs(left-right) > 1) return false;
                map.put(node, Math.max(left, right)+1);
            }else{
                if(node.left!=null && !map.containsKey(node.left)){
                    stack.push(node);
                    stack.push(node.left);
                }else{
                    stack.push(node);
                    stack.push(node.right);
                }
            }
        }
        return true;
    }

}

111. Minimum Depth of Binary Tree

二叉树的最小深度

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

Note: A leaf is a node with no children.

Example 1:

https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg

Input: root = [3,9,20,null,null,15,7]
Output: 2

Example 2:

Input: root = [2,null,3,null,4,null,5,null,6]
Output: 5

leetcode代码

/**
 * 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 int minDepth(TreeNode root) {
        if(root == null) return 0;
        int left = minDepth(root.left);
        int right = minDepth(root.right);
        return (left == 0 || right == 0) ? left + right + 1: Math.min(left,right) + 1;
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/152444.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Linux常用命令——nl命令

在线Linux命令查询工具(http://www.lzltool.com/LinuxCommand) nl 在Linux系统中计算文件内容行号 补充说明 nl命令读取 file 参数&#xff08;缺省情况下标准输入&#xff09;&#xff0c;计算输入中的行号&#xff0c;将计算过的行号写入标准输出。在输出中&#xff0c;n…

牛啊~ 长这么大还是头一次见24W字的SpringBoot从入门到实战文档

牛啊&#xff01;长这么大还是头一次见24W字的SpringBoot从入门到实战文档&#xff01; 不服还真不行&#xff0c;因为这份文档包含的内容是又全又新&#xff0c;而且还特别高深&#xff0c;从入门到实战的内容全都有&#xff01;&#xff01; 继续往下看&#xff1a; 本文档从…

EMQ设定ACL规则来控制客户端对主题的订阅权限

实现原理&#xff1a;EMQ可以通过制订ACL规则&#xff0c;校验客户端每个发布(Publish)/订阅(Subscribe) 的权限 本次采用的是EMQ Mnesia ACL。使用 EMQ X 内置的 Mnesia 数据库存储 ACL 规则 测试过程&#xff1a; 1本次实验环境&#xff0c;1个服务端&#xff08;发布1个主…

NumPy一维数组、二维数组与Pandas的Series、DataFrame行列结构和横纵方向的统一说明

最近在这个问题上耽误了一些时间&#xff0c;原因是之前个人理解上出了一些偏差&#xff0c;又受到错误文章的误导&#xff0c;把这个问题搞复杂了&#xff0c;现在统一梳理一下。在展开之前&#xff0c;先明确说明的是&#xff1a;NumPy的二维数组与Pandas的DataFrame&#xf…

7、Javaweb_tomcatservlet

web相关概念 1. 软件架构 1. C/S&#xff1a;客户端/服务器端 2. B/S&#xff1a;浏览器/服务器端 2. 资源分类 1. 静态资源&#xff1a;所有用户访问后&#xff0c;得到的结果都是一样的&#xff0c;称为静态资源.静态资源可以直接被浏览器解析 * 如&am…

2023年最全盘点 | 16款跨平台应用程序开发框架

近年来小程序技术被微信及其生态应用带的如火如荼的&#xff0c;开发者的世界里&#xff0c;小程序的技术非常受关注&#xff0c;特别在流量承接及跨端开发方面&#xff0c;均受到不同规模的企业关注及积极实践。 2023年&#xff0c;小程序依然很火&#xff0c;但却有了一些不…

元宇宙产业委MCC|于佳宁:加密资产摆脱不了周期性规律

2022年&#xff0c;加密行业面临寒冬&#xff0c;加密货币市场总价值大量缩水&#xff0c;降逾1.45万亿美元&#xff0c;期间多家加密公司接连倒塌&#xff0c;引发市场连锁效应。 加密货币总市值降逾1.45万亿美元 根据CoinMarketCap数据&#xff0c;加密货币总市值(Total Cry…

【服务器数据恢复】某品牌DS系列服务器RAID5数据恢复案例

服务器数据恢复环境&#xff1a; 某单位一台某品牌DS系列服务器连接4个扩展柜&#xff1b; 50块磁盘组建两组RAID5&#xff0c;其中一组由27块磁盘组建的RAID5存放的是Oracle数据库文件&#xff1b; 上层一共划分11个卷。 服务器故障&#xff1a; 磁盘故障导致存放Oracle数据库…

JavaScript类和对象

1、面向对象与面向过程 1.1 面向过程编辑POP(Process-oriented programming) 面向过程就是分析出解决问题所需要的步骤&#xff0c;然后用函数把这些步骤一步一步实现&#xff0c;使用的时候再一个一个的依次调用就可以了。面向过程&#xff0c;就是按照我们分析好的步骤&…

20张图带你了解JVM运行时数据区

运行时数据区总览 内存是非常重要的系统资源&#xff0c;是硬盘和CPU的中间仓库及桥梁&#xff0c;承载着操作系统和应用程序的实时运行。JVM内存布局规定了Java在运行过程中内存申请、分配、管理的策略&#xff0c;保证了JVM的高效稳定运行。不同的JVM对于内存的划分方式和管…

C 程序设计教程(11)—— 字符数据的输入与输出

C 程序设计教程&#xff08;11&#xff09;—— 字符数据的输入与输出 该专栏主要介绍 C 语言的基本语法&#xff0c;作为《程序设计语言》课程的课件与参考资料&#xff0c;用于《程序设计语言》课程的教学&#xff0c;供入门级用户阅读。 目录C 程序设计教程&#xff08;11&…

【2004NOIP普及组】T2.花生采摘 试题解析

【2004NOIP普及组】T2.花生采摘 试题解析 时间限制: 1000 ms 内存限制: 65536 KB 【题目描述】 鲁宾逊先生有一只宠物猴,名叫多多。这天,他们两个正沿着乡间小路散步,突然发现路边的告示牌上贴着一张小小的纸条:“欢迎免费品尝我种的花生!——熊字”。 鲁宾逊先生…

Android---Chip

Chip Chip 代表一个小块中的复杂实体&#xff0c;如联系人。它是一个圆形按钮&#xff0c;由一个标签&#xff0c;一个可选的芯片图标和一个可选的关闭图标组成。如果 Chip 可检查&#xff0c;则可以点击或切换Chip 。 style"style/Widget.MaterialComponents.Chip.Action…

疫情在家搭建的简单易学的SLAM建图机器人

1 简介 Easy_mqOS 是我仿照ROS 搭建的基于MQTT的简易机器人分布式开发框架,是一种轻量级并且十分容易上手的框架&#xff0c;支持多个节点的主题的订阅和单topic发布,节点之间独立、解耦合。没有复杂的文件配置&#xff0c;一定的make编程基础&#xff0c;像正常启动服务一样&a…

Redis未授权访问漏洞(四)SSH key免密登录

前言 系列文章 Redis未授权访问漏洞(一)先导篇 Redis未授权访问漏洞(二)Webshell提权篇 Redis未授权访问漏洞(三)Redis写入反弹连接定时任务 SSH key免密登录 实战实验 环境准备 实验前我们先来复习一遍ssh-key免密登录的流程 攻击机&#xff1a; Centos7 IP:192.168.142.44 靶…

局部变量的特点以及成员变量的区别

1. 概念在上面的章节中&#xff0c;其实已经跟大家介绍了局部变量的概念。即&#xff1a;局部变量是在定义形参、方法或代码块内部的变量&#xff0c;该变量只在当前方法、代码块中有效。2. 特点局部变量具有如下特点&#xff1a;● 局部变量声明在方法、构造方法或者代码块、形…

Mask RCNN网络源码解读(Ⅴ) --- Mask R-CNN论文解读环境配置以及训练脚本解析

目录 1.源码地址 2.项目配置 2.1 环境配置 2.2 文件结构 2.3 预训练权重下载地址&#xff08;下载后放入当前文件夹中&#xff09; 2.4 数据集&#xff1a;本例程使用的有COCO2017数据集和Pascal VOC2012数据集 2.4.1 COCO2017数据集 2.4.2 Pascal VOC2012数据集 2…

matplotlib+cartopy+geopandas,实现专业地图可视化!

知乎上有人问如何实现精细化地图&#xff1f;现有的excel、tableau、powerbi都只能套用有限的模板&#xff0c;是否有工具能实现高度定制化&#xff1f;除了专业的Gis软件外&#xff0c;我能想到相对完美的就是使用Python来实现。如果想制作出版级的地图可视化图表&#xff0c;…

《UEFI内核导读》UEFI Application Binary Interface (ABI)简介

敬请关注&#xff1a;“固件C字营 UEFI根据CPU体系结构和编译器的不同有着不同的“调用约定”统称之为“EFI ABI”。以MSVC和x86/x64举例来说&#xff0c;默认MSVC/x86使用 “C标准cdecl”&#xff0c;MSVC/x64使用“MSVC x64 ABI”。Gcc/x86使用“C标准cdecl”&#xff0c;Gc…

ESP-IDF:使用vector和deque容器进行打分排序例程

ESP-IDF实现例程&#xff1a; /5位选手&#xff0c;分别打十个分数&#xff0c;取中间8个分数&#xff0c;求平均值&#xff0c;然后根据选手的分数排序输出/ #include <stdio.h> #include using namespace std; #include #include #include #include class playe…