当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢
5G China unicom 直放站 网管协议 实时性要求
行程和用户[阅读理解法]
FFmpeg multimedia file processing (implementation of ffmpeg operation directory and list)
GET POST PUT DELETE request in GIN
gin's middleware and routing grouping
阿里大淘系模型治理阶段性分享
乐东消防救援大队应邀为干部开展消防安全培训
uni-app - uview Swiper 轮播图组件点击跳转链接(点击后拿到 item 行数据, 取出数据做操作)
error Trailing spaces not allowed no-trailing-spaces 9:14 error Unexpected trailing comma
Jenkins API groovy调用实践: Jenkins Core Api & Job DSL创建项目
随机推荐
某高校的R语言数据分析期末作业
ftplib+ tqdm 上传下载进度条
Realization of RTSP Protocol
GIN文件上传与返回
Professor Chen Qiang's "Machine Learning and R Application" course Chapter 15 Homework
FFMPEG多媒体文件处理(ffmpeg文件的删除与重命名)
群组行动控制--自动队列化实现策略
JS本地存储 sessionStorage和localStorage
为什么文字不贴合边
行程和用户[阅读理解法]
Sandbox中的进程/线程相关-1
ArcEngine(十)创建矢量图层
第六届“强网杯”全国网络安全挑战赛
IDEA Gradle 常遇问题(一)
绘制混合密度函数图以及添加分位数线
GIN中GET POST PUT DELETE请求
现在40系显卡都快出来了,为何1060型号的显卡还有这么多人用?
缓存和数据库一致性问题
Dry+Bean+Dataset R语言数据分析,报告英文
安踏携手华为运动健康共同验证冠军跑鞋 创新引领中国体育