剑指offer55.2 平衡二叉树

Proteus 8.9仿真五路抢答器:从74LS148编码到NE555报警的保姆级电路拆解 本文详细解析了使用Proteus 8.9仿真五路抢答器的完整设计流程,从74LS148优先编码器到NE555定时报警电路。通过保姆级的电路拆解和芯片选型指南,帮助读者掌握数字电路中的编码、锁存、译码和定时等核心技术,实现稳定可靠的抢答系统仿真。 阅读详情

剑指offer55.2 平衡二叉树

     输入一棵二叉树的根结点,判断该树是不是平衡二叉树。

     如果某二叉树中任意结点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。

注意:

     规定空树也是一棵平衡二叉树。

样例

输入:二叉树[5,7,11,null,null,12,9,null,null,null,null]如下所示,
    5
   / \
  7  11
    /  \
   12   9
输出:true

思路1:

     递归判断,分别求得左子树和右子树的深度,然后判断其差值是否小于1。

AcWing-72 C++ code:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int TreeDepth(TreeNode* root){
        if(root == NULL){
            return 0;
        }
        int nleft = TreeDepth(root->left);
        int nright = TreeDepth(root->right);
        return (nleft > nright) ? nleft + 1 : nright + 1; 
    }
    bool isBalanced(TreeNode* root) {
        if(root == NULL){
            return true;
        }
        int nleft = TreeDepth(root->left);
        int nright = TreeDepth(root->right);
        if(abs(nleft - nright) > 1){
            return false;
        }
        return isBalanced(root->left) && isBalanced(root->right);
    }
};

思路2:

    后续遍历的方法去遍历其左右子树,判断其左右子树是不是平衡二叉树,并且用depth分别去记录其左右子树的深度。

AcWing-72 C++ code:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isBalanced(TreeNode* root, int &depth){
        if(root == NULL){
            return true;
        }
        int left = 0, right = 0;
        if(isBalanced(root->left, left) && isBalanced(root->right, right)){
            if(abs(left - right) <= 1){
                depth = (left > right) ? left + 1 : right + 1;
                return true;
            }
        }
        return false;
    }
    bool isBalanced(TreeNode* root) {
        if(root == NULL){
            return true;
        }
        int depth = 0;
        return isBalanced(root, depth);
    }
};

SR-IOV 网卡虚拟化技术 目录 文章目录目录PCI 与 PCIe 设备SR-IOV在 KVM 中启用 SR-IOV 网卡手动挂载 VF 到虚拟机指令方式挂载SR-IOV 的数据包分发机制 PCI 与 PCIe 设备 PCI(Peripheral Component Interconnect,外设组件互连):符合 PCI 总线标准的设备就被称为 PCI 设备,PCI 总线架构中可以包含多个 PCI 设备。 PCIe(Per...... 阅读详情

相关推荐

上下文理解的AI增强方案:AI应用架构师的架构洞察

本文将从AI应用架构师的视角,深入探讨“上下文理解”的技术本质、核心挑战,以及一套可落地的上下文增强架构方案。我们会拆解上下文理解的底层需求,分析架构设计中的关键决策,并通过具体案例展示如何构建一个能“记住过去、理解现在、预测未来”的AI系统。三个维度:上下文的内容范围(短期/中期/长期)、数据类型(文本/结构化/多模态)、技术挑战(窗口限制/信息过载/跨会话一致性);三种架构模式:分层上下文存储(热/冷数据分离)、上下文路由(按需筛选)、动态压缩(适配模型窗口);四个关键组件。

欢迎来到我的优快云空间!这里聚焦AI大模型应用实战,分享前沿技术、实战案例与开发经验。 220

剑指 Offer 55 - II. 平衡二叉树

剑指 Offer 55 - II. 平衡二叉树 题目描述: 输入一棵二叉树的根节点,判断该树是不是平衡二叉树。如果某二叉树中任意节点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。 示例: 示例 1: 给定二叉树 [3,9,20,null,null,15,7] 返回 true 示例 2: 给定二叉树 [1,2,2,3,3,null,null,4,4] 返回 false 思路: 思路:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。遍历左子树和右

weixin_43825727的博客 123

Dia 使用说明

简述 在linux上面能找到dia这个工具还是挺幸运的,因为之前画流程图总是使用ubuntu默认的word工具 在里面画图,感觉流程图使用起来麻烦,并且视觉效果真的不好看。 自从使用Dia后,感觉至少画起来流程图会变得越来越标准了。但是Dia也存在问题,就是使用的文档很少,使用中遇到为后,查起来比较费劲,但是至少问题不是很多,所以使用中遇到的几点问题我就记下来,和大家分享,加快大家的开发和文档整理能力。 我会按照遇到的问题来说,不存在使用的先后顺序。 1. Dia 去掉背景中的线条 首先去点那个大一点的蓝色

linan101的博客 4926

剑指offer--55.II 平衡二叉树(思路搞明白)

题目描述 输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。 思路 递归解法分别求出左子树、右子树的深度,比较左子树、右子树的深度,选出最大的一个,再加1就是二叉树的深度 解决方法 /** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) {

Star_M的博客 194

详解如何用爬虫工具批量采集阿里巴巴批发网商品数据

本文主要介绍如何使用后羿采集器的智能模式,免费采集阿里巴巴批发网商品的批发价格、发货时间、是否代发等信息。 采集字段: 商品标题、商品链接、图片链接、标签1、标签2、标签3、价格、30天成交数、 评价、店铺 功能点目录: 如何对采集字段进行配置 如何采集列表+详情页类型网页 采集结果预览: 下面我们来详细介绍一下如何免费采集1688批发网的商品数据,我们以“羽绒服女”为例,具体步骤如下: 步骤一...

weixin_43777427的博客 1万+

2022年电赛E题声源定位跟踪系统

电赛声源定位

qq_53357019的博客 2万+

【剑指offer】55-2.平衡二叉树

题目描述 输入一棵二叉树的根节点,判断该树是不是平衡二叉树。如果某二叉树中任意节点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。 示例 1: 给定二叉树 [3,9,20,null,null,15,7] 3 / \ 9 20 / \ 15 7 返回 true 。 示例 2: 给定二叉树 [1,2,2,3,3,null,null,4,4] 1 / \ 2 2 / \ 3 3 / \ 4 4

Eamon 628

使用 Python 构建 Reddit 热帖爬虫:抓取热门讨论帖和评论

在现代互联网中,Reddit 被誉为全球最大、最活跃的讨论平台之一,汇聚了各种主题的帖子、评论和社交互动。因此,抓取 Reddit 上的热门帖子和评论可以为我们提供一个丰富的文本数据源,可以用于情感分析、话题建模、趋势分析等应用。Reddit 提供了官方的 API,可以让开发者获取包括帖子、评论、用户信息等在内的各种数据。我们可以结合上述方法,抓取多个热门帖子,并将每个帖子的标题、作者、发布时间和评论内容保存到一个数据结构中。每个帖子的对象包含了丰富的信息,如标题、内容、发布时间、作者等。

2201_76125261的博客 383

CS-Notes 剑指 Offer 55.2 详解:平衡二叉树的判定与自底向上后序遍历解法

本文基于 CS-Notes 仓库中 [剑指 Offer 55.2 平衡二叉树题解](https://link.gitcode.com/i/7f21467397524f3121e07ec5905e8158) 展开,围绕“判断一棵二叉树是否为平衡二叉树”这一经典面试问题,完整讲解平衡树的定义、自顶向下与自底向上两种解法的复杂度差异、原题 O(n) 解法的逐行实现与提前剪枝原理,并串联仓库内的二叉树深度

gitblog_00885的博客 357

Thor芯片部署

英伟达Jetson Thor芯片性能分析与应用现状 摘要:英伟达最新发布的Jetson Thor芯片基于Blackwell架构,官方宣称AI算力达2070TFLOPS(FP4),较前代Orin提升7.5倍,配备14核Arm CPU和128GB内存。然而实际量产版算力缩水至700TOPS,且因4nm制程良率问题多次延期至2025-2026年交付。该芯片支持FP4/FP8量化、Transformer引擎等先进特性,适用于自动驾驶、机器人等边缘AI场景,但受供应链限制,国内厂商倾向自研方案。开发者可通过JetPa

xiaomu_347的博客 436

五步通关!宝塔Linux部署ASP.NET Core的魔法秘籍大公开!

摘要: 本文提供ASP.NET Core应用五步部署攻略,涵盖从环境配置到线上运行的完整流程: 1️⃣ 环境准备:安装宝塔面板、ASP.NET Core运行时及开放端口; 2️⃣ 应用发布:通过dotnet publish生成独立部署包并上传服务器; 3️⃣ Nginx配置:设置反向代理将域名请求转发至应用端口; 4️⃣ 进程守护:使用Supervisor确保应用持续运行; 5️⃣ HTTPS加密:通过宝塔快速申请SSL证书并解决验证路径冲突。 关键点: 强调权限设置(755)、端口开放及反向代理细节; 提

java专栏 970

剑指offer55.2 平衡二叉树

题目描述: 输入一棵二叉树,判断该二叉树是否是平衡二叉树。 题目链接: 剑指offer55.2 public class Solution { boolean isBalanced = true; public boolean IsBalanced_Solution(TreeNode root) { getDepth(root); return is...

a282608054的博客 159

AMBA-CHI协议详解(七)

本节描述该协议包含的支持系统保序需求的机制。

Crazzy_M的博客 1161

【剑指offer】面试题55-2 平衡二叉树

题目要求 输入一棵二叉树,判断该二叉树是否是平衡二叉树。 平衡二叉搜索树(Self-balancing binary search tree)又被称为AVL树(有别于AVL算法),且具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树 解题分析 基于第55题【剑指offer】面试题55 二叉树的深度的基础,我们可以分别求出左右子树的深度,然后判断...

爱喝舒化奶的博客 522

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

剑指offer. 55-2 平衡二叉树

剑指offer. 55-2平衡二叉树 题目描述: 输入一棵二叉树,判断该二叉树是否是平衡二叉树。 解题思路: 利用后序遍历二叉树的每个节点,同时在遍历每个节点的时候记录它的深度 代码: class Solution { public: bool IsBalanced_Solution(TreeNode* pRoot) { int depth; ...

speargod的博客 174

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

剑指offer 55-2:平衡二叉树

此题是判断是否是二叉树 递归检查左、右子树的深度之差是否在1以内,但是在编程时有多种方式,应采用由底向上的方式进行,避免重复进行计算。 /** *Copyright @ 2019 Zhang Peng. All Right Reserved. *Filename: *Author: Zhang Peng *Date: *Version: *Description: **/ #include&lt...

Ben 186

【剑指offer】55-2、平衡二叉树

题目 输入一棵二叉树的根结点,判断该树是不是平衡二叉树。如果某二叉树中任意结点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。 解题思路 在二叉树深度的基础上修改:计算树的深度,树的深度=max(左子树深度,右子树深度)+1。在遍历过程中,判断左右子树深度相差是否超过1,如果不平衡,则令树的深度=-1,用来表示树不平衡。 最终根据树的深度...

火山君的博客 229

【剑指 Offer 题解】55.2 平衡二叉树

题目 输入一棵二叉树,判断该二叉树是否是平衡二叉树。 思路 一棵二叉树是否为平衡二叉树 = Math.abs(左子树的深度 - 右子树的深度) <= 1 public boolean IsBalanced_Solution(TreeNode root) { if (root == null) return true; int leftDepth = dep...

qingqingxiangyang的博客 180

【剑指Offer系列55-2】平衡二叉树

文章目录题目代码PythonC++ 题目 输入一棵二叉树的根节点,判断该树是不是平衡二叉树。如果某二叉树中任意节点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。 示例 1: 给定二叉树 [3,9,20,null,null,15,7] 3 / 9 20 / 15 7 返回 true 。 示例 2: 给定二叉树 [1,2,2,3,3,null,null,4,4] 1 / 2 2 / 3 3 / 4 4 返回 false 。 限制: 1 <= 树的结点个数 <=

小陈说CV的博客 178

剑指offer 55-2-平衡二叉树 C++

题目描述 输入一棵二叉树,判断该二叉树是否是平衡二叉树。 在这里,我们只需要考虑其平衡性,不需要考虑其是不是排序二叉树。 知识点:平衡二叉树的左右子树深度最多相差1;前面几层满秩。 参考书中解法! 思路1:遍历树的每个节点时,调用TreeNodeDepth函数得到它的左、右子树的深度。缺点:至上而下,判断上层节点时,会重复遍历下层节点 class Solution { public: bool IsBalanced_Solution(TreeNode* pRoot) { if (pRoot =

hly453的博客 244

【剑指Offer】55.2 平衡二叉树

NowCoder 题目描述 平衡二叉树左右子树高度差不超过 1。 public class Solution { // 求深度 public int TreeDepth(TreeNode root) { return root == null ? 0 : Math.max(TreeDepth(root.left)+1, TreeDepth(root.right)+1); } public boolean IsBalanced_Solution(TreeNod

瑞新の博客:bennyrhys 913

剑指 offer 55 - 2 平衡二叉树(递归)

1 题目描述 2 算法思路 思路: 此题基于上一题,《求二叉树的深度》,在此基础上只需要进行稍微修改即可 同样是后序遍历二叉树,求出每个节点的左右子树的深度, 如果深度差 < 2,就返回深度 如果深度差 > 2,就返回 -1 递归算法: 终止条件 当root == null 时,代表越过了叶子节点,返回0 如果左右子树的深度 == -1,代表已经不是平衡二叉树了,直接返回-1 返回值: 当 深度差 < 2时,返回深度 max(depthL.

Y_M的博客 228

剑指 Offer 55 - 2 平衡二叉树

输入一棵二叉树的根节点,判断该树是不是平衡二叉树。如果某二叉树中任意节点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。 示例 1: 给定二叉树 [3,9,20,null,null,15,7] 3 / 9 20 / 15 7 返回 true 。 示例 2: 给定二叉树 [1,2,2,3,3,null,null,4,4] 1 / \ 2 2 / \ 3 3 / 4 4 返回 false 。 限制: 0 <= 树的结点个数 <= 10000 /**

weixin_42414127的博客 144

[剑指offer] 55-2. 平衡二叉树

题目 输入一棵二叉树的根结点,判断该树是不是平衡二叉树。 如果某二叉树中任意结点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。 注意:规定空树也是一棵平衡二叉树。 v1:后序遍历+剪枝 后序遍历,从底至顶,返回子树深度,若判定某子树不是平衡树则 “剪枝” ,即,直接返回。 depth(root) 函数:计算树root的深度+判断子树平衡 返回值: 当节点root 左 / 右子树的深度差...

Alin_Zhang 164

剑指 offer第55.2题-平衡二叉树

平衡二叉树左右子树高度差不超过 1。 分别计算左右子树的高度,若是差值不超过1,则为平衡二叉树。这是最直接的做法,遍历每个结点,借助一个获取树深度的递归函数,根据该结点的左右子树高度差判断是否平衡,然后递归地对左右子树进行判断。 public classSolution { public boolean IsBalanced_Solution(TreeNode root) { ...

ritagreen123的博客 156

《剑指Offer》55.2 平衡二叉树

题目链接 牛客网 题目描述 平衡二叉树左右子树高度差不超过 1。 输入一棵二叉树,判断该二叉树是否是平衡二叉树。 解题思路 public class Solution { public boolean IsBalanced_Solution(TreeNode root) { if (root==null) return true; return Math.a...

Melo 164
上一篇: 剑指offer55.1 二叉树的深度
下一篇: 剑指offer56.1 数组中只出现一次的两个数字
WangXu__
博客等级 码龄9年 3粉丝 156原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值