二叉树的后序遍历
给出一棵二叉树,返回其节点值的后序遍历。
给出一棵二叉树 {1,#,2,3}
,
1 \ 2 / 3
返回 [3,2,1]
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/*
* @param root: A Tree
* @return: Postorder in ArrayList which contains node values.
*/
void houxu(vector<int>&a,TreeNode *root)
{
if(root==NULL)return ;
houxu(a,root->left);
houxu(a,root->right);
a.push_back(root->val);
}
vector<int> postorderTraversal(TreeNode * root) {
// write your code here
vector<int>a;
houxu(a,root);
return a;
}
};