LeetCode 145. Binary Tree Postorder Traversal
Solution1:我的答案
二叉树的后序遍历递归版是很简单的,关键是迭代版的代码既难理解又难写!
迭代版链接:https://blog.csdn.net/allenlzcoder/article/details/79837841
/** * 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) { vector<int> res; my_Postordertraversal(root, res); return res; } void my_Postordertraversal(TreeNode* root, vector<int>& res) { if (root) my_Postordertraversal(root->left, res); if (root) my_Postordertraversal(root->right, res); if (!root) return; res.push_back(root->val); } };