Binary Tree Postorder Traversal

二叉树的后序遍历
递归实现

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution
{
public:
    void PostOrder(TreeNode* root,vector<int>&vec)
    {
        if(root!=NULL)
        {
            PostOrder(root->left,vec);
            PostOrder(root->right,vec);
            vec.push_back(root->val);
        }
    }
    vector<int> postorderTraversal(TreeNode *root)
    {
         vector<int>vec;
         PostOrder(root,vec);
         return vec;
    }
};

  

posted @ 2014-07-31 10:10  calmound  阅读(102)  评论(0编辑  收藏  举报