LintCode-劍指Offer-(68)二叉樹的後序遍歷

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */
class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: Postorder in vector which contains node values.
     */
public:
    vector<int> postorderTraversal(TreeNode *root) {
        // write your code here
        static vector<int> pos;
        getpos(pos,root);
        return pos;

    }
    void getpos(vector<int> &pos,TreeNode *root){
        //static vector<int> pos;
        if(root==NULL)return ;
        postorderTraversal(root->left);
        postorderTraversal(root->right);
        pos.push_back(root->val);
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章