当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢
安踏携手华为运动健康共同验证冠军跑鞋 创新引领中国体育
Periodic sharing of Alibaba Da Tao system model governance
搭建大型分布式服务(四)Docker搭建开发环境安装Mysql
5G Unicom Network Management Design Ideas
为什么文字不贴合边
Anta and Huawei Sports Health jointly verify the champion running shoes and lead Chinese sports with innovation
剑指offer,剪绳子2
FPGA-近日工作总结
render解析
Come and throw eggs.
随机推荐
技嘉显卡 RGBFusion 不能调光解决方法
jenkins api创建自定义pipeline
Q_06_05 文件结构
NFS pays special attention to the problem of permissions
Draw a histogram with plot_hist_numeric()
error Trailing spaces not allowed no-trailing-spaces 9:14 error Unexpected trailing comma
【NVIDIA】Tesla V100安装NVIDIA-Driver驱动程序适配CUDA-Toolkit-11.6
Professor Chen Qiang's "Machine Learning and R Application" course Chapter 14 Assignment
Jenkins API groovy调用实践: Jenkins Core Api & Job DSL创建项目
[极客大挑战 2019]Upload
Final assignment of R language data analysis in a university
The sword refers to Offer 56 - II. Number of occurrences of a number in an array II (bit operation)
CPU-MIPS32 instruction architecture (unlocked pipeline microprocessor)
R语言kaggle 游戏数据探索与可视化
周末看点回顾|我国IPv6网络全面建成;2022昇腾AI开发者创享日·南京站成功举办…
Periodic sharing of Alibaba Da Tao system model governance
【瑞吉外卖】day05:增、删、改、查分类以及公共字段自动填充
快来扔鸡蛋。
leetcode 20. Valid Parentheses 有效的括号(中等)
5G China unicom AP:B SMS ASCII Transcoding Requirements