leetcode——145——Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values.

For example:
Given binary tree {1,#,2,3},

   1
    \
     2
    /
   3

return [3,2,1].


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

    }
};


你可能感兴趣的:(leetcode——145——Binary Tree Postorder Traversal)