Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1 \ 2 / 3
return [3,2,1]
.
Note: Recursive solution is trivial, could you do it iteratively?
给定一个二叉树,返回后序遍历节点的值。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
vector<int> result;
void postorder(TreeNode *root)
{
if(root==NULL) return;
postorder(root->left);
postorder(root->right);
result.push_back(root->val);
}
public:
vector<int> postorderTraversal(TreeNode *root) {
postorder(root);
return result;
}
};