当前位置:网站首页>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;
}
}
边栏推荐
- Cluster construction based on kubernetes v1.24.0 (I)
- 【C语言】详细的文件操作相关知识
- Is it safe for Huatai Securities to open a remote account? Is there any guarantee?
- 深圳地铁12号线第二批工程验收通过 预计7月28日试运行
- The sixth question of ape Anthropology
- Use abp Zero builds a third-party login module (4): wechat applet development
- Raspberry pie self built NAS cloud disk -- automatic data backup
- 基于Kubernetes v1.24.0的集群搭建(一)
- Take chef and ansible as examples to get started with server configuration
- Leetcode's 302 weekly rematch
猜你喜欢

Everything about native crash

sql的where+or的用法丢失条件

English语法_不定代词 - 概述

Prototype inheritance

Implement is by yourself_ default_ constructible

Native Crash的一切

使用TypeFace设置TextView的文字字体

【Rust】引用和借用,字符串切片 (slice) 类型 (&str)——Rust语言基础12

6-16 vulnerability exploitation -rlogin maximum permission login

It is difficult for Chinese consumers and industrial chains to leave apple, and iPhone has too much influence
随机推荐
Is it safe for Huatai Securities to open a remote account? Is there any guarantee?
Taishan Office Technology Lecture: layout difficulties of paragraph borders
Is there a free and commercially available website for US media video clips?
Zabbix5.0.8-odbc monitoring Oracle11g
猿人学第六题
Try... Finally summary
Everything about native crash
如何用WebGPU流畅渲染百万级2D物体?
猿人学第七题
SSM在线租房售房平台多城市版本
The second batch of projects of Shenzhen Metro Line 12 passed the acceptance and is expected to be put into trial operation on July 28
English语法_不定代词 - 概述
Summary of recent interviews
Reserved instances & Savings Plans
Implementing deep learning framework from zero -- further exploration of the implementation of multilayer bidirectional RNN
Wechat applet generates QR code
Custom scroll bar
如何使用autofs挂载NFS共享
The seventh topic of ape Anthropology
Solutions to problems in IE6 browser