当前位置:网站首页>Write down the post order traversal of the ~ binary tree
Write down the post order traversal of the ~ binary tree
2022-04-23 09:01:00 【Xiaoqiang~】
Writing to remember ~ Postorder traversal of binary trees
0、 Preface
This blog is to buckle 145. Postorder traversal of binary trees Problem solving , The title is simple. , Blogging is mainly because it can also be used as DFS Examples ~
Preorder traversal of binary tree Click here to To view the !
The middle order traversal of binary tree can Click here to To view the !
1、 Title Description

2、 Their thinking
2.1 Method 1 ~ recursive
2.1.1 Ideas
The post order traversal of binary tree is based on the access The left subtree – Right subtree – The root node Traverse the target tree in a way , When accessing the left subtree or right subtree, you can traverse in the same way , Until you walk through the whole tree . In combination with this approach, recursion can be used directly !
2.1.2 Program code
/** * 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) {} * }; */
class Solution {
public:
void postorder(TreeNode* root, vector<int>& ans)
{
if(!root)
return;
postorder(root->left,ans);
postorder(root->right,ans);
ans.push_back(root->val);
}
vector<int> postorderTraversal(TreeNode* root) {
vector<int> ans;
postorder(root,ans);
return ans;
}
};
2.2 Method 2 ~ DFS
2.2.1 Ideas
What about this idea , Is based on the force given on the buckle DFS Templates 2 Written by . Although it feels more like iteration , harm !
The idea here is equivalent to the method 1 The train of thought unfolds , From implicit stack to explicit stack , Everything else is the same , The specific implementation can be seen in the following code . But when expanding here, you need to judge the right subtree ! See the following procedure for details
2.2.2 Program code
/** * 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) {} * }; */
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> ans;
stack<TreeNode*> st;
TreeNode* cur = root;
TreeNode* prev;
while(cur || !st.empty())
{
while(cur)
{
st.push(cur);
cur = cur->left;
}
TreeNode* top = st.top();
if(top->right == nullptr || top->right == prev)
{
ans.push_back(top->val);
st.pop();
prev = top;
}
else
cur = top->right;
}
return ans;
}
};
版权声明
本文为[Xiaoqiang~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230726592713.html
边栏推荐
- Please arrange star trek in advance to break through the new playing method of chain tour, and the market heat continues to rise
- PLC point table (register address and point table definition) cracking detection scheme -- convenient for industrial Internet data acquisition
- Cadence process angle simulation, Monte Carlo simulation, PSRR
- Redis Desktop Manager for Mac
- 资源打包关系依赖树
- 关于堆的判断 (25 分) 两种插入方式
- 应纳税所得额
- 数字政府建设中政务中台中的技术创新点
- Wechat: get the owner of a single tag
- Use include in databinding
猜你喜欢
随机推荐
Brush classic topics
Valgrind and kcache grind use run analysis
Latex paper typesetting operation
Idea package jar file
2022-04-22 OpenEBS云原生存储
L2-024 tribe (25 points) (and check the collection)
idea打包 jar文件
Production practice elk
After a circle, I sorted out this set of interview questions..
MYCAT configuration
Introduction to matlab
企业微信应用授权/静默登录
Correct method of calculating inference time of neural network
Go语言自学系列 | golang结构体作为函数参数
Solidity 问题汇总
政务中台研究目的建设目标,建设意义,技术创新点,技术效果
Go language self-study series | golang structure as function parameter
MATLAB入门资料
Harbor enterprise image management system
Notes on xctf questions









