当前位置:网站首页>二叉树的深度遍历
二叉树的深度遍历
2022-07-16 04:42:00 【疯狂的仔】
用于个人记录
1)前序遍历
递归:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> ans = new ArrayList<>();
preorder(root, ans);
return ans;
}
public void preorder(TreeNode root , List<Integer> list){
if(root == null) return;
//处理单层逻辑
list.add(root.val);
preorder(root.left, list);
preorder(root.right, list);
}
}迭代:
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> ans = new ArrayList<>();
if(root == null)return ans;
LinkedList<TreeNode> stack = new LinkedList<>();
stack.addLast(root);
while(!stack.isEmpty()){
TreeNode temp = stack.removeLast();
ans.add(temp.val);
if (temp.right!=null)stack.addLast(temp.right);
if (temp.left!=null)stack.addLast(temp.left);
}
return ans;
}
}2)后序遍历
递归:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> ans = new ArrayList<>();
backorder(root, ans);
return ans;
}
public void backorder(TreeNode root, List<Integer> list){
if(root == null ) return ;
//
backorder(root.left, list);
backorder(root.right, list);
list.add(root.val);
}
}迭代:
可以由前序遍历稍微改点代码而来: 前序(中左右)---->改变前序遍历中某个节点的左右节点加入栈的顺序(即变为中右左)--->反转最终list中的顺序(即变为左右中)
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> ans = new ArrayList<>();
if(root == null)return ans;
LinkedList<TreeNode> stack = new LinkedList<>();
stack.addLast(root);
while(!stack.isEmpty()){
TreeNode temp = stack.removeLast();
ans.add(temp.val);
if (temp.left!=null)stack.addLast(temp.left);
if (temp.right!=null)stack.addLast(temp.right);
}
Collections.reverse(ans);
return ans;
}
}3)中序遍历
递归:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> ans = new ArrayList<>();
inorder(root, ans);
return ans;
}
public void inorder(TreeNode root, List<Integer> list){
if(root == null) return;
//
inorder(root.left, list);
list.add(root.val);
inorder(root.right, list);
}
}迭代:
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> ans = new ArrayList<>();
if(root == null) return ans;
LinkedList<TreeNode> stack = new LinkedList<>();
while(!stack.isEmpty() || root!=null){
while(root!=null){
stack.addLast(root);
root = root.left;
}
root = stack.removeLast();
ans.add(root.val);
root = root.right;
}
return ans;
}
}边栏推荐
- XML文件删除掉注释
- Solution -- Exploration
- The difference between function and method in rust
- Summary of application packaging and multi terminal compatibility
- Idea merges dev branch code into master and so on
- Audio focus arbitration strategy
- PMP practice once a day | don't get lost in the exam -7.14
- 三极管的基础知识(下)②
- The digital transformation forum for small and medium-sized enterprises in Shandong Province was successfully held, and Jiuzhou cloud empowers small and medium-sized enterprises to upgrade their digit
- 剑指Offer19-正则表达式匹配-字符串-动态规划
猜你喜欢

in_ Example analysis of the third parameter of array

Idea merges dev branch code into master and so on

Les employés de Tencent postent pour trouver des objets, ce qui indique une préférence pour les programmeurs! Les commentaires sont en feu... 丨 Black Horse Headlines

Golang --------- xiaoshiniudao gin framework file upload

【MySQL】——数据库的基本查询练习

Matlab机械臂建模运动学仿真+轨迹规划

Can ping command still play like this?

Feign 实现服务间并且调用时传递 header

数字孪生工厂丨智慧工厂孪生驾驶舱,实现智能化精益生产管理

Filebeat collects kubernetes cluster logs
随机推荐
Micro, m3o micro service series (I)
Opencv: convert video into continuous image frames
T100接口开发步骤简介
Two low-cost ways to attract users with integral check-in
Summary of application packaging and multi terminal compatibility
ping 命令还能这么玩?
OpenCv:将视频转化为连续的图像帧
marginalization
职场必备 | 123页华为内部项目管理PPT
What is the master-slave replication principle of MySQL
T100debug操作记录
Matlab科研绘图颜色补充(特别篇6)—336种法国传统颜色
Keep an IT training diary 068- a little unbalanced in my heart
基于JSP+Servlet的高校人事管理系统
Introduction to T100 interface development steps
Video processing: video sampling
ORA-600:[qertbGetPartitionNumber:qesma2],[],[],[]
Notes on logical problem solving in English reading
Opencv:05 filter
PMP每日一练 | 考试不迷路-7.15