当前位置:网站首页>94. Middle order traversal of binary tree
94. Middle order traversal of binary tree
2022-07-27 00:08:00 【happykoi】
94. Middle order traversal of binary trees
date :2022/7/17
Title Description : Given the root node of a binary tree root , return its Middle preface Traverse .
Example :
Input :root = [1,null,2,3]
Output :[1,3,2]
Input :root = []
Output :[]
Input :root = [1]
Output :[1]
Ideas :
Recursion and backtracking
Code + analysis :
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
recursive
*/
class Solution {
public:
void inorder(TreeNode* root, vector<int>& res){ //res The address of
if(root == nullptr) return;
inorder(root->left,res);
res.push_back(root->val); // Middle preface
inorder(root->right,res);
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
inorder(root,res);
return res;
}
};
// iteration
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
stack<TreeNode*> stk; // Stack ( Last in, first out )
while (root != nullptr || !stk.empty()) {
while (root != nullptr) { // Always traverse the left subtree
stk.push(root);
root = root->left;
}
root = stk.top(); // Back to previous
stk.pop(); // Pop up the top node, That is, the leftmost one of the subtree
res.push_back(root->val);
root = root->right; // Start traversing the right subtree of the node
}
return res;
}
};
Summary of learning :
- In the sequence traversal ( recursive + iteration )
- res In front &
边栏推荐
- Oracle remote connection configuration
- [2016] [paper notes] differential frequency tunable THz technology——
- Dynamic SQL
- In simple terms, cchart's daily lesson - Lesson 59 of happy high school 4 comes to the same end by different ways, and the C code style of the colorful interface library
- 第2章 开发用户流量拦截器
- Baidu website Collection
- 告别宽表,用 DQL 成就新一代 BI
- 08_ Event modifier
- Number that cannot be bought
- [literature reading] an investigation on hardware aware vision transformer scaling
猜你喜欢

C语言数组

07 design of ponding monitoring system based on 51 single chip microcomputer

2. Realize the map of navigation bar and battle page

会议OA之我的会议

Use Arthas to locate online problems

Azure synapse analytics Performance Optimization Guide (3) -- optimize performance using materialized views (Part 2)

Analysis of encoding and decoding of encode() and decode(), common encoding and why encode and decode are needed

MVC three-tier architecture

Dynamic SQL
![[2016] [paper notes] differential frequency tunable THz technology——](/img/7e/71126950250997fc436a4ee730aee7.png)
[2016] [paper notes] differential frequency tunable THz technology——
随机推荐
Azure synapse analytics Performance Optimization Guide (4) -- optimize performance using result set caching
4-4 对象生命周期
第7章 课程总结
Oracle remote connection configuration
04 traditional synchronized lock
[C language] classic recursion problem
MySQL数据库复杂操作:数据库约束,查询/连接表操作
What are the use cases in the Internet of things industry in 2022?
Tensorflow2.0 深度学习运行代码简单教程
np.transpose & np.expand_dims
Part II - C language improvement_ 7. Structure
At 12:00 on July 17, 2022, the departure of love life on June 28 was basically completed, and it needs to rebound
第二部分—C语言提高篇_12. 动/精态库的封装和使用
Practice of data storage scheme in distributed system
Everything you should know about wearable NFT!
The NFT market pattern has not changed. Can okaleido set off a new round of waves?
Part II - C language improvement_ 10. Function pointer and callback function
C语言数组
Last week's hot review (7.11-7.17)
What is Tencent cloud lightweight application server? What are the differences between CVM and ECS?