当前位置:网站首页>NC192 二叉树的后序遍历
NC192 二叉树的后序遍历
2022-08-09 13:02:00 【syc596】
NC192 二叉树的后序遍历
二叉树的后序遍历_牛客题霸_牛客网 (nowcoder.com)
145. 二叉树的后序遍历
// //递归
// import java.util.*;
// public class Solution {
// public void postorder(TreeNode root,List<Integer> list){
// if(root==null){
// return;
// }
// postorder(root.left,list);
// postorder(root.right,list);
// list.add(root.val);
// }
// public int[] postorderTraversal (TreeNode root) {
// List<Integer> list=new ArrayList<>();
// postorder(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[] postorderTraversal (TreeNode root) {
List<Integer> list=new ArrayList<>();
Stack<TreeNode> st=new Stack<TreeNode>();
TreeNode prev=null;
while(root!=null||st.isEmpty()==false){
while(root!=null){
st.push(root);
root=root.left;
}
TreeNode cur=st.pop();
if(cur.right==null||cur.right==prev){
list.add(cur.val);
prev=cur;
}else{
st.push(cur);
root=cur.right;
}
}
//
int[] ret=new int[list.size()];
for(int i=0;i<list.size();i++){
ret[i]=list.get(i);
}
return ret;
}
}边栏推荐
- Unicom network management protocol block diagram
- jenkins api create custom pipeline
- FPGA-近日工作总结
- Rmarkdown Tutorial
- 陈强教授《机器学习及R应用》课程 第十六章作业
- kustomize入门示例及基本语法使用说明
- kustomize entry example and basic syntax instructions
- puzzle(016.5)逻辑电路
- 19、学习MySQL 索引
- 5G China unicom repeater network management protocol real-time requirements
猜你喜欢
随机推荐
WPF 系统托盘 图标闪烁
GIN Bind模式获取参数和表单验证
绘制混合密度函数图以及添加分位数线
Anta and Huawei Sports Health jointly verify the champion running shoes and lead Chinese sports with innovation
JS本地存储 sessionStorage和localStorage
FFmpeg多媒体文件处理(ffmpeg操作目录及list的实现)
时间序列分析课程实验报告
技嘉显卡 RGBFusion 不能调光解决方法
kustomize entry example and basic syntax instructions
快来扔鸡蛋。
Process/Thread related in Sandbox - 1
乐东消防救援大队应邀为干部开展消防安全培训
陈强教授《机器学习及R应用》课程 第十五章作业
FFmpeg多媒体文件处理(ffmpeg打印音视频Meta信息)
5G China unicom AP:B SMS ASCII 转码要求
昇腾AI开发者创享日南京站!一起CANN机器狗+AI机械臂实现硬核智慧救援!燃爆现场~
LeetCode 37. Solve Sudoku
现在40系显卡都快出来了,为何1060型号的显卡还有这么多人用?
万物皆可柯里化的 Ramda.js
GIN初探,环境安装









