当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢
随机推荐
JS动画函数封装
Sandbox中的进程/线程相关-2
面试攻略系列(四)-- 你不知道的大厂面试
Professor Chen Qiang "application in machine learning and R" course chapter 17
FFMPEG多媒体文件处理(ffmpeg文件的删除与重命名)
How to solve the 0x80070005 error when the computer is reinstalled and the system is restored
Oracle Recovery Tools修复空闲坏块
陈强教授《机器学习及R应用》课程 第十五章作业
Sandbox中的进程/线程相关-1
Time series analysis course lab report
read stream special attention
用plot_hist_numeric()实现画直方图
LeetCode 37.解数独
R language kaggle game data exploration and visualization
基于 R 语言的深度学习——简单回归案例
搭建大型分布式服务(四)Docker搭建开发环境安装Mysql
Jenkins API groovy调用实践: Jenkins Core Api & Job DSL创建项目
陈强教授《机器学习及R应用》课程 第十四章作业
FFmpeg multimedia file processing (implementation of ffmpeg operation directory and list)
万物皆可柯里化的 Ramda.js