/**
* Definition for binary tree
* 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;
dfs(root, res);
return res;
}
void dfs(TreeNode *root, vector<int> &path) {
if (root == NULL)
return;
if (root->left != NULL)
dfs(root->left, path);
if (root->right != NULL)
dfs(root->right, path);
path.push_back(root->val);
}
};
http://oj.leetcode.com/problems/binary-tree-postorder-traversal/
Binary Tree Postorder Traversal
最新推荐文章于 2020-08-12 15:10:13 发布