#Leetcode# 145. Binary Tree Postorder Traversal

https://leetcode.com/problems/binary-tree-postorder-traversal/

 

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

Example:

Input: [1,null,2,3]
   1
    \
     2
    /
   3

Output: [3,2,1]

Follow up: Recursive solution is trivial, could you do it iteratively?

代码:

/**
 * 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> postorderTraversal(TreeNode* root) {
        vector<int> ans;
        if(!root) return ans;
        helper(root, ans);
        return ans;
    }
    void helper(TreeNode *root, vector<int> &ans) {
        if(root) {
            helper(root -> left, ans);
            helper(root -> right, ans);
            ans.push_back(root -> val);
        }
    }
};

  这个就是后序遍历为什么是 Hard 一定是有更快的写法吧 哭唧唧 太菜了 什么都不会

posted @ 2018-12-19 17:02  丧心病狂工科女  阅读(106)  评论(0编辑  收藏  举报