LeetCode 145. Binary Tree Postorder Traversal

匿名 (未验证) 提交于 2019-12-03 00:30:01

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);     } };
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!