/**
* 94. 二叉树的中序遍历
* @author wsq
* @date 2020/10/04
给定一个二叉树,返回它的中序 遍历。
示例:
输入: [1,null,2,3]
1
\
2
/
3
输出: [1,3,2]
链接:https://leetcode-cn.com/problems/binary-tree-inorder-traversal
*/
package com.wsq.tree;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
public class InorderTraversal {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> ans = new ArrayList();
Deque<TreeNode> stack = new LinkedList();
TreeNode pre = null;
TreeNode curr = root;
while(curr != null || !stack.isEmpty()){
while(curr != null){
stack.push(curr);
curr = curr.left;
}
curr = stack.pop();
ans.add(curr.val);
curr = curr.right;
// pre = curr;
// if(curr.right != null && curr.right != pre){
// curr = curr.right;
// }else{
// curr = null;
// }
}
return ans;
}
}
相关推荐
实战拆解:RAG vs Agent在金融风控中的性能对比
2024年上半年我帮某华东地区城商行做零售交易反欺诈系统升级时,技术团队曾发生过一次激烈争论:一派认为应该优先上RAG(检索增强生成)方案,成本低、落地快、合规性好;另一派坚持要上Agent(智能体)方案,推理能力强、能应对复杂新型欺诈。最终我们做了完整的AB测试,结论是两者没有绝对的优劣,只有场景适配的差异:RAG适合高并发、低时延的实时风控场景,Agent适合低并发、高准确率要求的事后核查/大额审批场景,两者结合的 hybrid 方案能覆盖95%以上的风控需求。本文我会结合真实项目的测试数据,从核心概念
【Day37】94.二叉树的中序遍历 递归+迭代遍历
94.二叉树的中序遍历 递归+迭代遍历
思博伦Spirent TestCenter创建单播流uni-stream
Traffic Generator列表中会出现刚才创建的uni-stream,创建完成。通常测试情况下,主要对Ethenet II和IPv4 Header进行配置。(通常情况下,如果流配置比较规范,选择任意端口接收,也没问题。编辑uni-stream基本信息。配置本条流由STC哪个端口接收。
Leetcode: NO.94 二叉树的中序遍历 迭代法
题目 给定一个二叉树,返回它的中序 遍历。 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 链接:https://leetcode-cn.com/problems/binary-tree-inorder-traversal 解题记录 递归方法 /** * @author: ffzs * @Date: 2020/9/14 上午7:27 */ class TreeNod
【Python&RS】哨兵2号(Sentinel2)轨道条带说明+图幅统计(表格+矢量)
今天给大家带来哨兵2号的图幅编号介绍以及全国省、市所需图幅数的统计。网上教程极少,而且结果也不公开。因为每次下载的时候都不知道需要几幅,所以研究后给大家分享一下。
【技术笔记】Cadence实现Orcad与Allegro软件交互式布局设置
为了达到原理图和PCB两两交互的功能,需要在ORCAD中将允许交互进行勾选,这样才可以,执行菜单命令Options-Preference后,我们打开参数界面,进行选择Miscellaneous菜单,将交互模式进行勾选。在做硬件pcb设计的时候,原理图选中一个元器件,希望可以再PCB中可以直接选中。对于网表必须是导入第一方网表,第三方是无效的。在allegro中使能/关闭交互式布局功能。
LeetCode:94. 二叉树的中序遍历(递归和迭代两种方法)
LeetCode:94. 二叉树的中序遍历(递归和迭代两种方法) /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), lef
Labview界面多按钮控件的一种处理方法
当界面中存在多个按钮控件时候,此时如果逐个为每个控件添加事件条件则会显得较为麻烦,此处提供了一种对多控件进行批量处理的方法
二叉树的中序遍历迭代算法例子详讲,LeetCode94
【代码】二叉树的中序遍历迭代算法例子详讲,LeetCode94。
Visual Studio 2022+Qt6.5.3安装教程+环境配置+创建Qt项目+乱码插件+运行很完美(16岁孩子也能看懂)
Visual Studio 2022+Qt6.5.3安装教程+环境配置+创建Qt项目+乱码插件+运行很完美(16岁孩子也能看懂)
手把手教你学Simulink——基于Simulink的隔离型DC-DC全桥变换器移相控制建模示例
本文详细介绍了基于Simulink的全桥移相变换器(PSFB)建模与控制方法。首先分析了全桥移相拓扑在大功率应用中的优势,包括ZVS实现、高频化和小型化特点。随后给出了具体设计参数和移相控制策略,采用电压外环+移相角内环的双闭环控制架构。文章重点演示了Simulink建模步骤,包括主电路搭建、移相PWM生成和PI控制器设计,并通过满载稳态、负载阶跃和轻载测试三种场景验证了系统性能。最后总结了工程实践要点,提出自适应控制等扩展方向,强调移相控制通过精准时间管理实现高效能量转换的核心价值。
94二叉树的中序遍历-迭代
题目描述 给定一个二叉树,返回它的中序 遍历。使用迭代方法。 思路分析 用栈来辅助,先入栈,再访问。 代码实现 public List<Integer> inorderTraversal(TreeNode root) { List<Integer> list =new LinkedList<>(); if(root!=nu...
DC Error: The power pin VDD on the cell UASA1/CG_X8_Rdsd2pcm is unconnected. (UPF-581)
【代码】DC Error: The power pin VDD on the cell UASA1/CG_X8_Rdsd2pcm is unconnected. (UPF-581)
94二叉树的中序遍历(递归法、迭代法)
1、题目描述 给定一个二叉树,返回它的中序遍历。 进阶:递归算法很简单,你可以通过迭代算法完成吗? 2、示例 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 3、题解 解法一: 基本思想:递归法中序遍历二叉树 解法二: 基本思想:迭代法基于栈中序遍历二叉树 #include<iostream>...
jre9下载 jdk9
(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】
廖雪峰Python教程[完整版]
廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白
如何用迭代法实现《94. 二叉树中序遍历》
你知道二叉树的中序遍历么?本文介绍如何使用 “递归算法” 和 “非递归算法” 对二叉树进行中序遍历。
SIMATIC S7-1200, S7-1500 PID 控制[手册]
SIMATIC S7-1200, S7-1500 PID 控制[手册]
力扣94.二叉树的中序遍历(递归and迭代法)(java)
但是找到最左边怎么回去呢?如果我在沿途找的时候,把过程中的结点存放起来,而栈最合适。因为如果想要实现左中右的遍历效果(也就是中序遍历效果),就需要先找到最左边的,遍历结果就存放在动态数组中,(ArrayList)迭代法就是模拟递归栈。
深度学习工具包 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');```[](https://bitdeli.com/free "Bitdeli Badge")
LeetCode 94 Binary Tree Inorder Traversal(二叉树的中序遍历)+(二叉树、迭代)
翻译给定一个二叉树,返回其中序遍历的节点的值。例如: 给定二叉树为 {1, #, 2, 3} 1 \ 2 / 3 返回 [1, 3, 2]备注:用递归是微不足道的,你可以用迭代来完成它吗?原文Given a binary tree, return the inorder traversal of its nodes' values.For example: Gi
力扣94.二叉树中序遍历:递归与迭代
本文介绍了二叉树中序遍历的两种实现方法:递归解法和迭代解法。递归解法采用分治策略,按"左-根-右"顺序遍历,代码简洁但存在栈溢出风险;迭代解法使用显式栈模拟递归过程,性能更稳定。两种方法时间复杂度均为O(n),空间复杂度为O(h)。递归适合学习和小规模数据,迭代适合生产环境。文章还提及Morris遍历和统一迭代法等扩展内容,建议根据实际需求选择合适方法,平衡代码可读性与算法性能。
Leetcode94:二叉树的中序遍历(迭代法)
public class Solution { public List < Integer > inorderTraversal(TreeNode root) { List < Integer > res = new ArrayList < > (); Stack < TreeNode > stack = new Stack < > (); TreeNode curr = root;//其实这
94. 二叉树的中序遍历(迭代遍历)
94. 二叉树的中序遍历 给定一个二叉树的根节点 root ,返回它的 中序 遍历。 示例 1: 输入:root = [1,null,2,3] 输出:[1,3,2] 思路和144 二叉树的前序遍历(迭代遍历)一致,但区别在于我们不是 遇见一个就将这个节点的值加入数组中,而是在这个节点出栈的时候,再将其节点加入数组。 很容易想到:什么情况下节点会出栈? 叶子节点a出栈:左节点为null,退回a,a出栈。 非叶子节点b出栈:b的左子树已经遍历完成,退回b,然后继续遍历b的右子树。 可以看到,某个节点出栈
94 二叉树的中序遍历(迭代法)
最终结果为:[4, 2, 5, 1, 3],与预期一致。中序遍历的正确结果是:[4, 2, 5, 1, 3]
leetcode 94 二叉树的中序遍历 递归和迭代
给定一个二叉树,返回它的中序 遍历。 示例: 输入: [1,null,2,3] 1 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 迭代 class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<>(); Stack<TreeNode
94. 二叉树的中序遍历(Swift实现, 迭代)
模拟递归压栈每次将当前节点压入栈中,然后移动到左子节点。这相当于递归调用处理左子树。模拟递归出栈当无法继续向左走时,弹出栈顶节点。处理该节点后,转向右子树。继续遍历将current指向右子节点,继续上述过程。
leetcode94.二叉树的中序遍历(迭代法)
给定一个二叉树,返回它的中序 遍历。 示例: 输入: [1,null,2,3] 1 2 / 3 输出: [1,3,2] /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class So
leetcode 94 二叉树的中序遍历迭代版
解法一: class Solution { public: void goLongLeftBranch(TreeNode* root,stack<TreeNode*> s) //与前序遍历的沿最左侧通路不同,它只记录而不访问 { while(root) { s.push(root); ...
leetcode94. 二叉树的中序遍历(迭代法实现)
给定一个二叉树的根节点 root ,返回 它的 中序 遍历。
252



被折叠的 条评论
为什么被折叠?



