二叉树的深度

C++求二叉树深度 开发工具与关键技术: Visual Studio / C++ 作者:何文涛 撰写时间:2019-8-05 二叉树的创建以及简单的运用我上次以及发布过了,现在做的是求二叉树的数深度。 下面就举了了例子来演示求二叉树的算法: //二叉树节点 typedef struct BINARYNODE { char ch; struct BINARYNODE* lchild; struct BINARYNOD... 阅读详情
/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    int TreeDepth(TreeNode* pRoot)
    {
        if(!pRoot)
            return 0;
        
        return 1+max(TreeDepth(pRoot->left),TreeDepth(pRoot->right));
    }
};

 

编程计算二叉树的深度 #include "stdio.h" #include "malloc.h" typedef struct BiTNode{     char data;   /*结点的数据域*/     struct BiTNode *lchild , *rchild;  /*指向左孩子和右孩子*/ } BiTNode , *BiTree; /*创建一棵二叉树*/ void CreatBiTree(BiTre... 阅读详情

相关推荐

2025.06.20【pacbio】|PB甲基化分析结果的统计与可视化介绍

本文介绍了PacBio甲基化数据的统计分析与可视化方法。主要内容包括:1) 分析甲基化位点分布、比例及染色体分布;2) 修饰基序(Motif)的统计与热图展示;3) 全基因组和局部区域测序深度可视化;4) 使用IGV查看单个reads修饰情况。文章提供了R代码示例,涵盖柱状图、分布图、热图等多种可视化形式,并建议了差异甲基化分析等进阶方法,帮助科研人员深入挖掘甲基化数据。

2067

数据结构实验 5.二叉树深度

【问题描述】 给定一棵二叉树,求该二叉树的深度。 二叉树深度定义:从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的节点个数为树的深度。 【输入形式】 第一行是一个整数n,表示二叉树的结点个数。二叉树结点编号从1到n,根结点为1,n <= 10 接下来有n行,依次对应二叉树的n个节点。 每行有两个整数,分别表示该节点的左儿子和右儿子的节点编号。如果第一个(第二个)数为-1则表示没有左(右)儿子。 【输出形式】 输出一个整型数,表示树的深度。 【样例输入】 3 2 3 -1 -1

Running Snail 804

Windows平台无人机仿真:从零搭建SITL环境与QGC联调

本文详细指导了在Windows平台上从零搭建无人机软件在环(SITL)仿真环境,并与QGroundControl(QGC)进行联调的完整流程。通过使用Cygwin工具链,开发者可以在没有实体硬件的情况下,模拟运行ArduPilot飞控,并通过MAVLink协议连接QGC地面站进行全功能测试,为无人机软件开发提供了一个安全、经济的虚拟沙盒环境。

weixin_29239279的博客 412

计算二叉树深度算法(递归、非递归)入门详解

二叉树在应用时,经常需要知道二叉树的深度。二叉树的深度就是二叉树的层数,即从树根算起,到最底下一层的层数是多少,即二叉树中结点的最大层次值。 本文给出了计算二叉树深度的算法,包括递归算法和非递归算法。......

sunnyoldboy博客 3万+

电子纸价签实战:基于SSD1680的墨水屏驱动开发与优化

本文详细介绍了基于SSD1680驱动芯片的电子纸价签开发实战,涵盖硬件连接、驱动框架搭建、核心流程解析及性能优化。重点剖析了初始化配置、波形文件加载、图像数据写入与刷新触发等关键步骤,并提供了功耗优化、显示效果提升及代码健壮性等实用避坑指南,帮助开发者高效实现稳定低功耗的墨水屏驱动。

weixin_28278377的博客 231

阻容降压 vs 开关电源:5V小功率供电方案成本与安全深度对比

本文深度对比了阻容降压电路与开关电源在5V小功率供电方案中的核心差异。重点剖析了二者在成本、体积、效率、温升及安全隔离等方面的优劣,并结合智能温控器、LED驱动等典型应用场景,为硬件工程师提供了清晰的选型决策框架,帮助在成本与安全之间做出最佳权衡。

weixin_29310631的博客 702

二叉树之二叉树的深度

二叉树之二叉树的深度

qq_43218521的博客 6202

weblogic启动异常故障处理(boot认证失败)

root 用户生成的文件属主为 root,WebLogic进程将无权读取,直接导致认证失败。· ⚠️ 密码必须完全一致:第3步 AdminAccount 命令中设置的密码,与第5步所有 boot.properties 文件中 password= 字段的值,必须保证完全一致。如果您的域下配置了受管服务器实例(如 ms_server1、ms_server2 等),必须逐一更新各实例下的免密启动文件,确保实例重启时密码与AdminServer保持一致,否则实例启动会因认证失败而报错。此步骤至关重要,必须执行。

weixin_53389944的博客 224

二叉树的深度和高度

给定一个二叉树, 找出其最大深度。二叉树的深度为根节点到最远叶子节点最长路径上的节点数。如二叉树 [ 3, 9, 20, null, null, 15, 7 ]的最大深度为3:先看一个简单的情况:对于节点 3, 最大深度自然是左右子节点 + 1, 左右子节点都有可能为空, 但只要有一个, 树的最大高度就是 1 + 1 = 2。然后再增加几个节点:而对于节点 3, 则是左右子树深度最大的那个然后 + 1, 具体谁更大, 并不关心。

Mamba_Stan的博客 1万+

基于Matlab模拟RANS湍流——详细分析与源代码

在模拟过程中,我们首先定义了模拟所需的参数,包括流体的动力黏度和密度,以及边界条件和网格分辨率。接下来,通过求解平均值方程和湍流涨落方程,我们模拟了湍流的演化过程。在本文中,我们将使用Matlab对RANS湍流进行模拟,并提供相应的源代码。RANS湍流模拟的基本思想是将流场的变量分解为平均值和涨落值,并通过求解平均值方程和湍流涨落方程来描述流场的演化。请注意,以上代码仅为示例,实际的RANS湍流模拟可能需要更复杂的算法和模型。接下来,我们将求解RANS方程,包括平均值方程和湍流涨落方程。

ByteKnight的博客 862

化工设计软件:AutoCAD Plant 3D二次开发_(1).AutoCADPlant3D概述

AutoCAD Plant 3D 是一款专为化工行业设计的三维工厂设计和建模软件。它基于AutoCAD平台,提供了强大的三维建模功能和专业的化工设计工具,帮助工程师和设计师在工厂设计过程中提高效率和准确性。AutoCAD Plant 3D不仅能够创建详细的三维模型,还可以进行管道设计、设备布置、支架设计等复杂任务。通过上述案例,我们可以看出 AutoCAD Plant 3D 在化工厂设计中的强大功能和优势。该软件不仅提供了丰富的建模工具和专业的设计功能,还支持二次开发,使用户能够根据具体需求进行定制化操作。

kkchenjj的博客 1109

二叉树(六):二叉树的最大深度与最小深度

一、二叉树的最大深度 二叉树的最大深度 二叉树的最大深度可以通过递归法,层次遍历和深度优先遍历实现,但是最简单最容易的就是递归法。 1、递归法 1)确定递归三要素 递归函数的返回值和参数:返回值为当前节点的最大深度,参数为当前节点 递归函数的单层逻辑:当前节点的最大深度 = 1 + 左子树或右子树的最大深度 递归函数的终止条件:当前节点为空时,当前节点最大深度为0 2)递归方法代码 class Solution { public: //递归参数及返回值 int maxDepth(Tre

qq_42138662的博客 9093

深度学习工具包 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")

计算二叉树的深度

[算法步骤] 如果是空树,递归结束,深度为0;否则执行一下操作 递归计算左子树的深度计为m; 递归计算右子树的深度计为n; 如果m大于n,二叉树的深度为m+1,否则为n+1; [算法描述] int Depth(BiTree T) { int m, n; if (T == NULL) return 0; //如果是空树,深度为0,递归结束 else { m = Depth(T->lchild); //递归计算左子树的深度记为m

qq_47142993的博客 5万+

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

二叉树的深度——C++

概念:二叉树的深度是指树中结点的最大深度(通俗的说 ,从根节点到最底层的叶子结点的行数便是二叉树的深度)。 C++代码: #define _CRT_SECURE_NO_WARNINNGS #include<iostream> using namespace std; //二叉树结点 typedef struct BINARYTREENODE { char root; struct...

ling08140814的博客 2386

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

二叉树的深度_二叉树的深度

题目描述输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。数据结构 TreeNode 的定义:牛客编程题高级数据结构格式说明_牛客博客​blog.nowcoder.net解题思路及代码方法一:分治法分治法的基本思想是:求一个规模为 n 的问题时,我们先求左边规模大约为 n/2 的问题,然后再求右边规模大约为 n/2 的问题,最后...

weixin_39798626的博客 1907

二叉树的深度和平衡二叉树的深度

二叉树的深度 虽然前面已经做了二叉树的深度,仍然不牢固。 剑指offer里面用的是递归的方法。只要的思路就是,左子树和右子树的比较,大的一支加1作为深度。并不是此时右子树的深度就不加一,现在考虑的是该节点的深度,而不是左右子树的,左右子树只是helper。该节点可能也是其他结点的左右结点之一。 借鉴的一个大神的图,很清晰: 也是相当于后序遍历。 def maxdepth(root): if not root: return 0 depthleft = maxdepth(root.left) de

RUN 1819

二叉树 | 二叉树的深度

文章目录问题:二叉树的最大深度解题思路C++代码 问题:二叉树的最大深度 问题链接 解题思路 采用递归的思路,如果root为NULL,返回0,否则返回1+max{左子树深度,右子树深度} C++代码 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *lef...

逐梦者 868

二叉树的深度的深度计算与完全二叉树、满二叉树概念

二叉树的深度计算 1、一颗树只有一个节点,它的深度是1; 2、二叉树的根节点只有左子树而没有右子树,那么可以判断,二叉树的深度应该是其左子树的深度加1; 3、二叉树的根节点只有右子树而没有左子树,那么可以判断,那么二叉树的深度应该是其右树的深度加1; 4、二叉树的根节点既有右子树又有左子树,那么可以判断,那么二叉树的深度应该是其左右子树的深度较大值加1。 int TreeDeep(struct n...

zqc的博客 5565

求二叉树深度以及寻找二叉树中某一节点值

size_t BTreeDepth(BTNode* root) BTNode* BTreeFind(BTNode* root, BTDataType x) 求二叉树的深度依然用的是二叉树递归的特性。二叉树的深度就是你根节点的这一层加上他左孩子或者右孩子中深度大的哪一个,同样这也能被划分为子问题。 size_t BTreeDepth(BTNode* root) { if (root == NULL) return 0; if (BTreeDepth(root-&gt;_left) &gt;

Hanani_Jia的博客 5022

二叉树深度计算

问题描述 https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/点击打开链接 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the r

ych_ding的专栏 2万+
上一篇: 和为S的两个数字
下一篇: 和为S的连续正数列
云水谣CS
博客等级 码龄9年 8粉丝 268原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值