当前位置:网站首页>NC193 二叉树的前序遍历
NC193 二叉树的前序遍历
2022-08-09 13:02:00 【syc596】
NC193 二叉树的前序遍历
二叉树的前序遍历_牛客题霸_牛客网 (nowcoder.com)
144. 二叉树的前序遍历
// //递归
// import java.util.*;
// public class Solution {
// public void preorder(TreeNode root,List<Integer> list){
// if(root==null){
// return;
// }
// list.add(root.val);
// preorder(root.left,list);
// preorder(root.right,list);
// }
// public int[] preorderTraversal (TreeNode root) {
// List<Integer> list=new ArrayList<>();
// preorder(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[] preorderTraversal (TreeNode root) {
// if(root==null){
// return new int[0];
// }
// List<Integer> list=new ArrayList<>();
// Stack<TreeNode> st=new Stack<>();
// st.push(root);
// while(st.isEmpty()==false){
// TreeNode cur=st.pop();
// list.add(cur.val);
// //根左右-栈-入栈先右后左
// if(cur.right!=null){
// st.push(cur.right);
// }
// if(cur.left!=null){
// st.push(cur.left);
// }
// }
// //
// int[] ret=new int[list.size()];
// for(int i=0;i<list.size();i++){
// ret[i]=list.get(i);
// }
// return ret;
// }
// }
//迭代-sameto中序
import java.util.*;
public class Solution {
public int[] preorderTraversal (TreeNode root) {
List<Integer> list=new ArrayList<>();
Stack<TreeNode> st=new Stack<>();
TreeNode cur=root;
while(cur!=null||st.isEmpty()==false){
while(cur!=null){
list.add(cur.val);
st.push(cur);
cur=cur.left;
}
cur=st.pop();
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 repeater network management protocol real-time requirements
- GIN初探,环境安装
- Jenkins API groovy调用实践: Jenkins Core Api & Job DSL创建项目
- The sword refers to the offer, cuts the rope 2
- FFmpeg多媒体文件处理(FFMPEG日志系统)
- FFmpeg multimedia file processing (the basic concept of ffmpeg processing stream data)
- 剑指 Offer 43. 1~n 整数中 1 出现的次数(递归、数学)
- FPGA - Summary of bugs in ISE (in update)
- Yocto 可以下载的第三方库
- Professor Chen Qiang "application in machine learning and R" course chapter 17
猜你喜欢
kustomize entry example and basic syntax instructions
GIN Bind模式获取参数和表单验证
error Trailing spaces not allowed no-trailing-spaces 9:14 error Unexpected trailing comma
FPGA-近日工作总结
FFmpeg multimedia file processing (the basic concept of ffmpeg processing stream data)
Map mixed density function and quantile added line
gin's middleware and routing grouping
The FPGA - work summary recently
Unicom network management protocol block diagram
电脑重装系统还原0x80070005错误如何解决
随机推荐
陈强教授《机器学习及R应用》课程 第十四章作业
43. The sword refers to Offer 1 ~ 1 the number of occurrences of n integers (recursive, mathematics)
Process/Thread related in Sandbox - 1
记录本项目中用到的系统调用与C库函数-2
FFmpeg multimedia file processing (the basic concept of ffmpeg processing stream data)
WPF 实现带蒙版的 MessageBox 消息提示框
问题系列-如何修改或更新localhost里的值
FPGA-近日工作总结
陈强教授《机器学习及R应用》课程 第十五章作业
【瑞吉外卖】day05:增、删、改、查分类以及公共字段自动填充
ArcEngine(九)图形绘制
FFmpeg multimedia file processing (implementation of ffmpeg operation directory and list)
How to solve the 0x80070005 error when the computer is reinstalled and the system is restored
javscript基础易错点集合
剑指 Offer 56 - II. 数组中数字出现的次数 II(位运算)
5G China unicom repeater network management protocol real-time requirements
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)
LeetCode 37.解数独
uni-app - uview Swiper 轮播图组件点击跳转链接(点击后拿到 item 行数据, 取出数据做操作)
5G China unicom AP:B SMS ASCII Transcoding Requirements