当前位置:网站首页>25. Middle order traversal of binary tree
25. Middle order traversal of binary tree
2022-07-24 12:46:00 【Little happy】
94. Middle order traversal of binary trees
The difficulty is simple 1030
Given the root node of a binary tree root , Back to its Middle preface Traverse .
Example 1:

Input :root = [1,null,2,3]
Output :[1,3,2]
Example 2:
Input :root = []
Output :[]
Example 3:
Input :root = [1]
Output :[1]
Example 4:

Input :root = [1,2]
Output :[2,1]
Example 5:

Input :root = [1,null,2]
Output :[1,2]
recursive
If the current node is not empty , Then recursively put the values of all nodes of the left subtree into ans in , Then put the value of the current node into ans in , Then recursively put the values of all nodes of the right subtree into ans in
Definition inorder(root) Indicates that the current traversal to \textit{root}root The answer to the node , So by definition , We just call recursively inorder(root.left) To traverse \textit{root}root The left subtree of the node , And then \textit{root}root The value of the node is added to the answer , And recursively call inorder(root.right) To traverse \textit{root}root The right subtree of the node can be , The condition for the termination of recursion is to hit an empty node .
Code
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
inorder(root, res);
return res;
}
public void inorder(TreeNode root, List<Integer> res) {
if (root == null) {
return;
}
inorder(root.left, res);
res.add(root.val);
inorder(root.right, res);
}
}
iteration ( Advanced ) Left root right ·
Press the leftmost chain of the whole lesson tree into the stack , Every time you take out the top element of the stack , And record the top element of the stack , If it has a right subtree , Then press the right subtree into the stack .
/** * 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<Integer>();
Stack<TreeNode> stk = new Stack<TreeNode>();
TreeNode p = root;
while(p!=null||!stk.isEmpty()){
while(p!=null){
stk.add(p);
p = p.left;
}
p = stk.pop();
ans.add(p.val);
p = p.right;
}
return ans;
}
}
边栏推荐
- EfficientFormer:轻量化ViT Backbone
- 树莓派自建 NAS 云盘之——树莓派搭建网络存储盘
- 基于matlab的声音识别
- 做自媒体视频剪辑有免费可商用的素材网站吗?
- No routines, no traps, no advertisements | are you sure you don't need this free instant messaging software?
- C language course design -- hotel management system
- Intent jump pass list set
- MobileViT:挑战MobileNet端侧霸主
- for mysql
- SSM在线租房售房平台多城市版本
猜你喜欢

基于Kubernetes v1.24.0的集群搭建(三)

Qt Creator怎样更改默认构建目录

Vscode solves the problem of terminal Chinese garbled code
![ERROR: [Synth 8-439] module ‘xxx‘ not found not found 错误解决办法](/img/47/bb03cc26e254332bf815c80bafb243.png)
ERROR: [Synth 8-439] module ‘xxx‘ not found not found 错误解决办法

C language course design -- hotel management system

English grammar_ Indefinite pronouns - Overview
![[rust] reference and borrowing, string slice type (& STR) - rust language foundation 12](/img/48/7a1777b735312f29d3a4016a14598c.png)
[rust] reference and borrowing, string slice type (& STR) - rust language foundation 12

基于matlab的声音识别

手把手教你用 Power BI 实现 4 种可视化图表

基于Qt的软件框架设计
随机推荐
Use abp Zero builds a third-party login module (4): wechat applet development
做自媒体视频剪辑有免费可商用的素材网站吗?
Is it safe to open an account on Oriental Fortune online? Is there a threshold for opening an account?
Use typeface to set the text font of textview
让一套代码完美适配各种屏幕
English语法_不定代词 - 概述
猿人学第七题
Industry insight | how to better build a data center? It and business should "go together"
6-16 vulnerability exploitation -rlogin maximum permission login
元宇宙更多的功能和作用在于对于传统生活方式和生产方式的深度改造
Leetcode's 302 weekly rematch
Why does 2.tostring() report an error
如何用WebGPU流畅渲染百万级2D物体?
Getting started with SQL join use examples to learn left connection, inner connection and self connection
The seventh topic of ape Anthropology
Solutions to problems in IE6 browser
Set up CI server with Jenkins
基于Kubernetes v1.24.0的集群搭建(一)
Ah, I am uncle card space
Buckle practice - maximum number of 28 splices