当前位置:网站首页>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;
}
}
边栏推荐
- FPGA-在ISE中错误总结(更新中)
- Process/Thread related in Sandbox - 1
- 01_iTween_第一天--小球抛物线
- kustomize entry example and basic syntax instructions
- 用plot_hist_numeric()实现画直方图
- error Trailing spaces not allowed no-trailing-spaces 9:14 error Unexpected trailing comma
- Professor Chen Qiang's "Machine Learning and R Application" course Chapter 14 Assignment
- GIN a preliminary study, the environment is installed
- 农村区县域农业电商如何做?数字化转型如何进行?
- 现在40系显卡都快出来了,为何1060型号的显卡还有这么多人用?
猜你喜欢
Come and throw eggs.
jenkins api create custom pipeline
How to reduce the size of desktop icons after the computer is reinstalled
关于做2D游戏时,Canvas边界显示在Game窗口的问题
现在40系显卡都快出来了,为何1060型号的显卡还有这么多人用?
GIN初探,环境安装
[MRCTF2020]套娃-1
搭建大型分布式服务(四)Docker搭建开发环境安装Mysql
FFmpeg multimedia file processing (implementation of ffmpeg operation directory and list)
5G China unicom repeater network management protocol real-time requirements
随机推荐
电脑重装系统还原0x80070005错误如何解决
kustomize入门示例及基本语法使用说明
阿里大淘系模型治理阶段性分享
Jenkins API groovy调用实践: Jenkins Core Api & Job DSL创建项目
puzzle(016.5)逻辑电路
jenkins api create custom pipeline
万物皆可柯里化的 Ramda.js
jenkins api创建自定义pipeline
电脑重装系统后桌面图标如何调小尺寸
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)
ARM板卡增加路由功能
FFmpeg multimedia file processing (implementation of ffmpeg operation directory and list)
CPU-MIPS32指令架构(无内锁流水线微处理器)
How to reduce the size of desktop icons after the computer is reinstalled
FFmpeg多媒体文件处理(ffmpeg打印音视频Meta信息)
剑指offer,剪绳子2
The sword refers to Offer 56 - II. Number of occurrences of a number in an array II (bit operation)
ArcEngine(十)创建矢量图层
handwritten big pile
力扣解法汇总1413-逐步求和得到正数的最小值