当前位置:网站首页>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
边栏推荐
- K210 learning notes (II) serial communication between k210 and stm32
- Pctp test experience sharing
- PLC的点表(寄存器地址和点表定义)破解探测方案--方便工业互联网数据采集
- Non duplicate data values of two MySQL query tables
- MATLAB入门资料
- Data visualization: use Excel to make radar chart
- Latex paper typesetting operation
- Notes d'apprentissage oneflow: de functor à opexprinterpreter
- Go语言自学系列 | golang方法
- DJ music management software pioneer DJ rekordbox
猜你喜欢
随机推荐
是否完全二叉搜索树 (30 分)
Go language self-study series | initialization of golang structure
Share the office and improve the settled experience
2022-04-22 openebs cloud native storage
L2-023 graph coloring problem (25 points) (graph traversal)
Summary of solid problems
计算神经网络推理时间的正确方法
Please arrange star trek in advance to break through the new playing method of chain tour, and the market heat continues to rise
【58】最后一个单词的长度【LeetCode】
Introduction to matlab
Is Zhongyan futures safe and reliable?
Single chip microcomputer nixie tube stopwatch
LeetCode396. Rotate array
Harbor enterprise image management system
GUI编程简介 swing
The K neighbors of each sample are obtained by packet switching
L2-3 浪漫侧影 (25 分)
Judgment on heap (25 points) two insertion methods
MATLAB入门资料
Valgrind and kcache grind use run analysis









