当前位置:网站首页>NC161 二叉树的中序遍历
NC161 二叉树的中序遍历
2022-08-09 13:02:00 【syc596】
NC161 二叉树的中序遍历
二叉树的中序遍历_牛客题霸_牛客网 (nowcoder.com)
94. 二叉树的中序遍历
// //递归
// import java.util.*;
// public class Solution {
// public void inorder(TreeNode root,List<Integer> list){
// if(root==null){
// return;
// }
// inorder(root.left,list);
// list.add(root.val);
// inorder(root.right,list);
// }
// public int[] inorderTraversal (TreeNode root) {
// List<Integer> list=new ArrayList<>();
// inorder(root,list);
// //
// int[] ret=new int[list.size()];
// for(int i=0;i<list.size();i++){
// ret[i]=list.get(i);
// }
// return ret;
// }
// }
//中序遍历-左根右
//迭代
import java.util.*;
public class Solution {
public int[] inorderTraversal (TreeNode root) {
if(root==null){
return new int[0];
}
List<Integer> list=new ArrayList<>();
Stack<TreeNode> st=new Stack<TreeNode>();
TreeNode cur=root;
while(cur!=null||st.isEmpty()==false){
while(cur!=null){
st.push(cur);
cur=cur.left;
}
cur=st.pop();
list.add(cur.val);
cur=cur.right;
}
//
int[] ret=new int[list.size()];
for(int i=0;i<list.size();i++){
ret[i]=list.get(i);
}
return ret;
}
}边栏推荐
- Dry+Bean+Dataset R语言数据分析,报告英文
- 43. The sword refers to Offer 1 ~ 1 the number of occurrences of n integers (recursive, mathematics)
- offset、client、scroll、window.pageYOffset比较
- jenkins api创建自定义pipeline
- Record the system calls and C library functions used in this project-2
- GIN a preliminary study, the environment is installed
- 【FPGA教程案例48】图像案例8——基于FPGA的RGB图像转化为HSV图像的实现,通过MATLAB进行辅助验证
- javscript基础易错点集合
- handwritten big pile
- How to reduce the size of desktop icons after the computer is reinstalled
猜你喜欢

5G China unicom 一般性异常处理

Map mixed density function and quantile added line

#WeArePlay | 与更多开发者一起,探索新世界

npm install失败

5G China unicom AP:B SMS ASCII Transcoding Requirements

jenkins api创建自定义pipeline

FFmpeg多媒体文件处理(ffmpeg操作目录及list的实现)

Unicom network management protocol block diagram

19、学习MySQL 索引

Unity3d_API_Gyroscope 陀螺仪的接口
随机推荐
RTSP协议讲解
The sword refers to the offer, cuts the rope 2
Time series analysis course lab report
剑指 Offer 57 - II. 和为s的连续正数序列(滑动窗口)
R 语言 2010.1至2021.12艾滋病每月发病人数 时间序列分析
面试攻略系列(三)-- 高级开发工程师面试问些啥?
kustomize entry example and basic syntax instructions
JS轮播图实现
[极客大挑战 2019]Upload
5G China unicom repeater network management protocol real-time requirements
GIN Bind模式获取参数和表单验证
FFmpeg多媒体文件处理(ffmpeg处理流数据的基本概念)
万物皆可柯里化的 Ramda.js
IDEA Gradle 常遇问题(二)(持续更新)
#WeArePlay | 与更多开发者一起,探索新世界
ArcEngine(十)创建矢量图层
第六届“强网杯”全国网络安全挑战赛
FPGA-在ISE中错误总结(更新中)
ftplib+ tqdm 上传下载进度条
陈强教授《机器学习及R应用》课程 第十七章作业