当前位置:网站首页>Likou - preorder traversal, inorder traversal, postorder traversal of binary tree
Likou - preorder traversal, inorder traversal, postorder traversal of binary tree
2022-08-05 02:43:00 【qq_52025208】
一、题目描述:给你二叉树的根节点 root ,返回它节点值的 前序 遍历.
1.递归:
class Solution {
List<Integer> list = new ArrayList<>();
public List<Integer> preorderTraversal(TreeNode root) {
if(root == null) return list;
list.add(root.val);
preorderTraversal(root.left);
preorderTraversal(root.right);
return list;
}
}
2.非递归
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list=new ArrayList<>();
if(root==null) return list;
Stack<TreeNode> stack=new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node= stack.pop();
list.add(node.val);
if(node.right!=null){
stack.push(node.right);
}
if(node.left!=null){
stack.push(node.left);
}
}
return list;
}
}
二、中序遍历
递归:
class Solution {
List<Integer> list = new ArrayList<>();
public List<Integer> inorderTraversal(TreeNode root) {
if(root == null) return list;
inorderTraversal(root.left);
list.add(root.val);
inorderTraversal(root.right);
return list;
}
}
非递归:
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if(root == null) return list;
Stack<TreeNode> stack = new Stack<>();
while(root != null || !stack.empty()) {
while(root!= null) {
stack.push(root);
root = root.left;
}
root = stack.pop();
list.add(root.val);
root = root.right;
}
return list;
}
}
三、后序遍历
class Solution {
List<Integer> list = new ArrayList<>();
public List<Integer> postorderTraversal(TreeNode root) {
if(root == null) return list;
postorderTraversal(root.left);
postorderTraversal(root.right);
list.add(root.val);
return list;
}
}
边栏推荐
- 回顾51单片机
- 海量服务实例动态化管理
- 力扣-二叉树的最大的深度
- 2022-08-04: Input: deduplicated array arr, the numbers in it only contain 0~9.limit, a number.Return: The maximum number that can be spelled out with arr if the requirement is smaller than limit.from
- RAID disk array
- 2022了你还不会『低代码』?数据科学也能玩转Low-Code啦!
- Error: Not a signal or slot declaration
- DAY23: Command Execution & Code Execution Vulnerability
- 22-07-31周总结
- 学习笔记-----左偏树
猜你喜欢
随机推荐
Opening - Open a new .NET modern application development experience
nodeJs--封装路由
注意潍坊开具发票一般需要注意
DAY22: sqli-labs shooting range clearance wp (Less01~~Less20)
LeetCode使用最小花费爬楼梯----dp问题
Industry case | insurance companies of the world's top 500 construction standards can be used to drive the business analysis system
Data to enhance Mixup principle and code reading
Regular expression to match a certain string in the middle
使用SuperMap iDesktopX数据迁移工具迁移ArcGIS数据
01 【前言 基础使用 核心概念】
你要的七夕文案,已为您整理好!
特殊矩阵的压缩存储
北斗三号短报文终端露天矿山高边坡监测方案
正则表达式,匹配中间的某一段字符串
2022-08-04: Input: deduplicated array arr, the numbers in it only contain 0~9.limit, a number.Return: The maximum number that can be spelled out with arr if the requirement is smaller than limit.from
Solve connect: The requested address is not valid in its context
Intel XDC 2022 Wonderful Review: Build an Open Ecosystem and Unleash the Potential of "Infrastructure"
[Decryption] Can the NFTs created by OpenSea for free appear in my wallet without being chained?
Simple implementation of YOLOv7 pre-training model deployment based on OpenVINO toolkit
后期学习计划