当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢
5G China unicom repeater network management protocol real-time requirements
电脑重装系统还原0x80070005错误如何解决
FPGA中串口通信的时钟频率和波特率计数
jenkins api create custom pipeline
WSA工具箱安装应用商店提示无法工作怎么解决?
jenkins api创建自定义pipeline
FFmpeg multimedia file processing (the basic concept of ffmpeg processing stream data)
Uni - app - uview Swiper shuffling figure component, click on the links to jump (click to get the item after the row data, remove data operation)
缓存和数据库一致性问题
微服务+微信小程序实现社区服务
随机推荐
Q_08 更多信息
Rmarkdown教程
Periodic sharing of Alibaba Da Tao system model governance
FFMPEG多媒体文件处理(ffmpeg文件的删除与重命名)
Professor Chen Qiang "application in machine learning and R" course chapter 17
FFmpeg multimedia file processing (ffmpeg prints audio and video Meta information)
行程和用户[阅读理解法]
Anta and Huawei Sports Health jointly verify the champion running shoes and lead Chinese sports with innovation
An Offer 21. Adjust the array in order to make odd in even the front (loop invariant)
telnet+ftp to control and upgrade the device
eslint语法规则报错
正则表达式-re模块
ArcEngine(八) 选择要素并高亮显示
RTSP协议的实现
render解析
19、学习MySQL 索引
FPGA-在ISE中错误总结(更新中)
FPGA中串口通信的时钟频率和波特率计数
Jenkins API groovy调用实践: Jenkins Core Api & Job DSL创建项目
【奖励公示】第23期 2022年7月奖励名单公示:社区明星评选 | 新人奖 | 博客同步 | 推荐奖