lintcode.68 二叉树后序遍历

二叉树的后序遍历 

 

给出一棵二叉树,返回其节点值的后序遍历。

样例

给出一棵二叉树 {1,#,2,3},

   1
    \
     2
    /
   3

返回 [3,2,1]

 

 

 

/**
 * Definition of TreeNode:
 * 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.
     */
     vector<int> l;
    vector<int> postorderTraversal(TreeNode * root) {
        // write your code here
        if(root==NULL)
            return l;
        postorderTraversal(root->left);
        postorderTraversal(root->right);
        l.push_back(root->val);
        return l;
    }
};

  

posted @ 2017-08-29 18:29  会飞的雅蠛蝶  阅读(133)  评论(0编辑  收藏  举报