Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree
Given binary tree
{1,#,2,3}
,1 \ 2 / 3
return
[1,2,3]
. /**
* 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:
void postorderTra(TreeNode* node, vector<int> &result) {
if(node == NULL) {
return;
}
postorderTra(node->left, result);
postorderTra(node->right, result);
result.push_back(node->val);
}
vector<int> postorderTraversal(TreeNode* root) {
vector<int> result;
postorderTra(root, result);
return result;
}
};
0 comments:
Post a Comment