二叉树的下一个结点

海康视觉平台VisionMaster实战入门:从通讯配置到工具应用 本文为海康VisionMaster视觉平台实战入门指南,详细介绍了从软件安装准备、与PLC等外部设备的通讯配置,到匹配定位、测量、畸变矫正等核心工具的应用方法。通过实战案例拆解,帮助工程师快速掌握如何搭建完整的机器视觉检测与引导定位系统,实现工业自动化项目落地。 阅读详情
题目:

给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。

解法:

图片来源:牛客网
如图所示,可以归纳出两种情况:1)给定节点有右儿子,则下一个节点是右儿子的最左边儿子 ,2)给点节点没有右儿子,则一直找到当前节点的父节点是其父节点的左儿子的节点,比如(j,H 节点)。


代码如下:
class TreeLinkNode {
    int val;
    TreeLinkNode left = null;
    TreeLinkNode right = null;
    TreeLinkNode next = null;

    TreeLinkNode(int val) {
        this.val = val;
    }
}

public class GetNext {

    public TreeLinkNode GetNext(TreeLinkNode pNode)
    {
        if(pNode == null) {
            return pNode;
        }
        if(pNode.right != null){
            pNode = pNode.right;
            while (pNode.left!=null){
                pNode = pNode.left;
            }
            return pNode;
        }
        while (pNode.next != null){
            if(pNode.next.left == pNode){
                return pNode.next;
            }
            pNode = pNode.next;
        }

        return null;

    }
}
立创开源便携式多生理参数监护仪:基于STM32H743与ADS1292R的心电血氧体温监测系统设计 本文详细解析了基于STM32H743与ADS1292R的便携式多生理参数监护仪开源设计。项目集成了心电、血氧和体温监测功能,采用模块化硬件架构,并重点介绍了利用FIR滤波和Pan-Tompkins算法进行心电信号处理的核心技术。文章提供了完整的复刻指南与避坑要点,是学习医疗电子设备设计的优秀实践案例。 阅读详情

相关推荐

Linux下离线安装Google Chrome

目录背景一、下载并安装Google Chrome二、下载并安装依赖包1.libvulkan.so.1()(64bit)2.读入数据总结 背景 项目中服务器被要求不能连接外网,不能直接联网安装,所以需要下载Google Chrome安装包和依赖包,然后离线安装。 linux下步骤都一样,不管ubuntu还是centos只是需要下载对应系统的文件包即可。 本机系统:CentOS 7 一、下载并安装Google Chrome 官方下载地址http://www.google.cn/chrome/browser/d

ancientapesman的博客 1万+

数据结构学习3——树

数据结构学习(三)树树的结构树的逻辑结构1) 基本术语2) 树的遍历树的存储结构 树的结构 树的逻辑结构 1) 基本术语 说明 树 子树、空树、有序树、无序树、森林 空树无结点,有序无序看子树交换顺序是不是同一棵 点 叶子结点、分支结点、根结点、孩子结点、双亲结点、兄弟结点、堂兄弟结点 度 结点的度、树的度 度即分叉数,最大的结点的度即树的度 路 路径长度、祖先、子孙 分支数为路径长度,路径若存在必唯一 层 结点的层数、树的深度/高度 层数从1开始 结点的层序编号

fangfang12138的博客 293

Oracle EBS R12 AP模块发票导入接口实战:从SQL插入到API调用的完整流程

本文详细解析了Oracle EBS R12 AP模块发票导入接口的完整流程,从SQL插入到API调用的实战技巧。涵盖接口表设计、数据验证、双通道导入机制及错误诊断,特别分享官方文档未提及的高阶优化方案,助力企业实现财务数字化转型。

weixin_33728268的博客 477

二叉树中序的下一个结点

题目 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。 考察对二叉树遍历的理解 # -*- coding:utf-8 -*- # class TreeLinkNode: # def __init__(self, x): # self.val = x # self...

北歌的博客 365

PyTorch实战:5步搞定Res2Net图像分类模型(附完整代码)

本文详细介绍了如何使用PyTorch在5步内构建高性能Res2Net图像分类模型,包括环境配置、核心模块实现、完整网络架构搭建、训练流程优化及模型部署技巧。Res2Net通过多尺度特征融合机制显著提升图像分类性能,附完整代码帮助开发者快速实践。

java5的博客 1024

Docker拉取ubuntu22.04镜像使用ROS2 humble及仿真工具可视化进行导航

Docker中安装Ubun22以及ROS,gazebo可视化等

lovebc的博客 1678

python 二叉树的下一个结点

''' 题目描述 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。 注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。 ''' # -*- coding:utf-8 -*- # class TreeLinkNode: # def __init__(self, x): # self.val = x # self.left ...

WYXHAHAHA123的博客 333

解密CANoe数据回放:从BLF到ASC的格式转换与优化策略

本文深入解析CANoe数据回放中BLF与ASC格式的转换与优化策略,帮助工程师高效处理汽车电子测试数据。详细介绍了两种格式的特性、转换方法及性能优化技巧,包括CANoe内置功能、命令行自动化和第三方工具应用,提升故障诊断和系统验证效率。

weixin_28331555的博客 538

【剑指offer】二叉树的下一个结点

题目描述 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。 解题思路 如果该结点有右孩子,则返回右子树的最左边的结点; 否则,则向上找该结点的父结点,如果父结点是上个父结点的右子树,则一直向上找,直到父结点是上个父结点的左子树,则返回父结点的父结点; 我也不知道我为什么写的那么啰嗦了,神经病。 我的代码 /*...

u010659877的专栏 126

2025年情感分析(SA)综述——论文阅读报告

2025年情感分析(SA)综述——论文阅读报告

Samdy_L的博客 2088

MATLAB基础应用精讲-【深度强化学习】SAC(Soft Actor-Critic)算法(附MATLAB和python代码实现)

Actor 网络:用于选择动作。Critic 1 和 Critic 2 网络:用于估计 Q 值。Target Critic 1 和 Target Critic 2:与 Critic 网络架构相同,用于生成更稳定的目标 Q 值。,即在保证探索的情况下选择高价值动作。软更新目标 Q 网络参数,使得目标网络参数缓慢向当前网络靠近,避免振荡。

qq_36130719的博客 454

【剑指Offer】二叉树的下一个结点

上图所示二叉树,找下一个结点,分四种情况考虑: 结点2——下一个结点为3 结点4——下一个结点为5 结点5——下一个结点为6 结点7——下一个结点为8 而下面所说的第二种情况类似于删除节点中的找后继节点,先判断是否存在右结点,再判断右结点是否存在左结点,如果存在左结点,就一直找到最下面的一个左结点,否则下一个结点就为右结点。 第一种情况是找父节点。 结点2和4都是在找父节点或者祖...

ncc1995的博客 285

图解ADAS电源系统架构:看懂这张图,搞懂前装摄像头的供电秘密!

别以为ADAS系统就是“摄像头 + SoC”,其实最难搞的是供电系统,尤其是要满足车规级ASIL-D安全等级时。供电时序、电压稳定性、电源监控、冗余通信链路,一个都不能少。😤一旦电源选型不对,轻则系统卡顿,重则全车失控,不是闹着玩的。

weixin_43199439的博客 676

57.二叉树的下一个结点

题目 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。 思路 中序遍历遍历的顺序是左中右。 如果一个结点的右孩子不为空,那么有两种情况。 右孩子没有左孩子,那么该右孩子即为下一个结点。 如果右孩子还有左孩子,那么一路找左孩子,找到最后一个。 如果右孩子为空,检查自己的父结点是不是某一个结点的左孩子。 如...

Dou 207

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

剑指offer JZ8 二叉树的下一个结点

接下来讨论没有右子树的情况,如果当前节点是父节点的左子节点那么返回当前节点的父节点,比如说d节点的下一个节点是b节点,如果当前节点是父节点的右子节点,那么就继续向上,回溯寻找下一个节点,直到找到当前节点是父节点的左子节点或父节点为空,返回当前节点的父节点,比如说i节点的下一个节点是a节点。需要注意的是需要考虑边界条件是否合法,树中只有一个节点的情况,当前节点是中序遍历的最后一个节点,是否能正确处理,并返回正确结果。树中从父节点指向子节点的指针用实线表示,从子节点指向父节点的用虚线表示。

qq_74980411的博客 1777

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

上一篇: 把数组排成最小的数
下一篇: 层序遍历二叉树类型
coder_flag
博客等级 码龄9年 2粉丝 53原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值