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:
    vector<int> res;
    void dfs(TreeNode *root){
        if(!root) return;
        dfs(root->left);
        dfs(root->right);
        res.push_back(root->val);
    }
    vector<int> postorderTraversal(TreeNode *root) {
        dfs(root);
        return res;
    }
};

 

posted @ 2015-03-16 21:14  SprayT  阅读(72)  评论(0编辑  收藏  举报