当前位置:网站首页>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
边栏推荐
- Enterprise wechat application authorization / silent login
- 求简单类型的矩阵和
- idea打包 jar文件
- Complete binary search tree (30 points)
- LaTeX论文排版操作
- LLVM之父Chris Lattner:编译器的黄金时代
- Failed to prepare device for development
- Initial experience of talent plan learning camp: communication + adhering to the only way to learn open source collaborative courses
- Go语言自学系列 | golang方法
- LaTeX数学公式
猜你喜欢
L2-024 部落 (25 分)(并查集)
资源打包关系依赖树
Valgrind and kcache grind use run analysis
[58] length of the last word [leetcode]
2021 Li Hongyi's adaptive learning rate of machine learning
L2-3 romantic silhouette (25 points)
使用flask和h5搭建网站/应用的简要步骤
PLC point table (register address and point table definition) cracking detection scheme -- convenient for industrial Internet data acquisition
idea打包 jar文件
L2-3 浪漫侧影 (25 分)
随机推荐
Non duplicate data values of two MySQL query tables
Star Trek's strong attack opens the dream linkage between metacosmic virtual reality
Output first order traversal according to second order and middle order traversal (25 points)
L2-3 浪漫侧影 (25 分)
Share the office and improve the settled experience
How to read excel table to database
Complete binary search tree (30 points)
Taxable income
Go language self-study series | golang nested structure
是否完全二叉搜索树 (30 分)
Failed to download esp32 program, prompting timeout
Wechat: get the owner of a single tag
Experimental report on analysis of overflow vulnerability of assembly language and reverse engineering stack
企业微信应用授权/静默登录
Withholding agent
Latex mathematical formula
Matlab draw five-star red flag
Resource packaging dependency tree
L2-024 部落 (25 分)(并查集)
Error: cannot find or load main class