当前位置:网站首页>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;
}
}边栏推荐
- telnet+ftp to control and upgrade the device
- Professor Chen Qiang's "Machine Learning and R Application" course Chapter 15 Homework
- ftplib+ tqdm 上传下载进度条
- 搭建大型分布式服务(四)Docker搭建开发环境安装Mysql
- 关于做2D游戏时,Canvas边界显示在Game窗口的问题
- FFmpeg多媒体文件处理(FFMPEG日志系统)
- 行程和用户[阅读理解法]
- The sword refers to the offer, cuts the rope 2
- Final assignment of R language data analysis in a university
- 01_iTween_第一天--小球抛物线
猜你喜欢

FFmpeg multimedia file processing (the basic concept of ffmpeg processing stream data)

Jenkins API groovy调用实践: Jenkins Core Api & Job DSL创建项目

The FPGA - work summary recently

面试攻略系列(四)-- 你不知道的大厂面试

Ledong Fire Rescue Brigade was invited to carry out fire safety training for cadres

kustomize入门示例及基本语法使用说明

农村区县域农业电商如何做?数字化转型如何进行?

Anta and Huawei Sports Health jointly verify the champion running shoes and lead Chinese sports with innovation

FPGA-近日工作总结

Clock frequency and baud rate count for serial communication in FPGA
随机推荐
问题系列-如何修改或更新localhost里的值
万物皆可柯里化的 Ramda.js
WSA工具箱安装应用商店提示无法工作怎么解决?
Jenkins API groovy调用实践: Jenkins Core Api & Job DSL创建项目
【奖励公示】第23期 2022年7月奖励名单公示:社区明星评选 | 新人奖 | 博客同步 | 推荐奖
技嘉显卡 RGBFusion 不能调光解决方法
FFmpeg multimedia file processing (implementation of ffmpeg operation directory and list)
WSA toolkit installed app store tip doesn't work how to solve?
Q_07 词汇表
ftplib+ tqdm upload and download progress bar
七夕力扣刷不停,343. 整数拆分(剑指 Offer 14- I. 剪绳子、剑指 Offer 14- II. 剪绳子 II)
5G China unicom AP:B SMS ASCII 转码要求
NFS pays special attention to the problem of permissions
Clock frequency and baud rate count for serial communication in FPGA
Record the system calls and C library functions used in this project-2
Final assignment of R language data analysis in a university
puzzle(016.5)逻辑电路
FPGA - Summary of bugs in ISE (in update)
FFMPEG multimedia file processing (deletion and renaming of ffmpeg files)
搭建大型分布式服务(二)搭建会员服务