来源:力扣 (LeetCode)
链接:https://leetcode-cn.com/problems/binary-tree-postorder-traversal
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
一、题目描述
给定一个二叉树,返回它的 后序 遍历。
例如输入 [1,null,2,3] :
|
1 2 3 4 5 |
1 \ 2 / 3 |
输出:
|
1 |
[3,2,1] |
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
二、题解
参考:二叉树的后序遍历。
三、代码
3.1 通过栈+链表实现
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: vector<int> postorderTraversal(TreeNode* root) { stack<TreeNode *> s; list<TreeNode *> l; vector<int> v; TreeNode *p; if (root == nullptr) { return v; } s.push(root); while (!s.empty()) { p = s.top(); s.pop(); l.push_front(p); if (p->left) { s.push(p->left); } if (p->right) { s.push(p->right); } } while(!l.empty()) { v.push_back(l.front()->val); l.pop_front(); } return v; } }; |
![[leetcode]145-二叉树的后序遍历](https://www.dyxmq.cn/wp-content/uploads/2020/02/ce5a3-image6343ae7c83fc33ae.png)
3.2 通过 2 次压栈实现
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
class Solution { public: vector<int> postorderTraversal(TreeNode* root) { TreeNode *p; vector<int> v; stack<TreeNode *> st; if (root == nullptr) { return v; } st.push(root); st.push(root); while (!st.empty()) { p = st.top(); st.pop(); if (!st.empty() && st.top() == p) { if (p->right) { st.push(p->right); st.push(p->right); } if (p->left) { st.push(p->left); st.push(p->left); } } else { v.push_back(p->val); } } return v; } }; |
![【每日打卡】[leetcode]-409最长回文子串](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/e5589-image788d511d7c1e839c.png&w=280&h=210&a=&zc=1)
![[leetcode]303-区域和检索(数组不可变)](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/02/eddfa-imagef16d1b42e00db2eb.png&w=280&h=210&a=&zc=1)

![[leetcode]7-整数反转](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/02/e23c0-image.png&w=280&h=210&a=&zc=1)

![[leetcode]199-二叉树的右视图](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/04/f24a8-image.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]72-编辑距离](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/04/88c60-imageff84a6c5047db6ed.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]460-LFU缓存](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/04/5b3f2-image.png&w=280&h=210&a=&zc=1)
![[leetcode]125-验证回文串](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/5bfa5-image.png&w=280&h=210&a=&zc=1)
![【每日打卡】[程序员面试宝典]17.16-按摩师](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/9ecc9-image.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]876-链表的中间节点](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/64d0d-imagece778ab033c9d90d.png&w=280&h=210&a=&zc=1)
![【每日打卡】[剑指offer]面试题40-最小的k个数](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/c83c9-imagec745c425c670e18c.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]面试题1.6-字符串压缩](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/776bc-image.png&w=280&h=210&a=&zc=1)
评论