[LeetCode] Binary Tree Postorder Traversal dfs,深度搜索

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].

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

 

Hide Tags
  Tree Stack
 
  一题后续遍历树的问题,很基础,统计哪里的4ms 怎么实现的。- -
 
#include <iostream>

#include <vector>

using namespace std;



/**

 * 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> postorderTraversal(TreeNode *root) {

        vector<int> ret;

        if(root==NULL)  return ret;

        help_f(root,ret);

        return ret;

    }

    void help_f(TreeNode *node,vector<int> &ret)

    {

        if(node==NULL)  return;

        help_f(node->left,ret);

        help_f(node->right,ret);

        ret.push_back(node->val);

    }

};



int main()

{

    return 0;

}

 

你可能感兴趣的:(LeetCode)