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;
}

 

相关文章:

  • 2022-12-23
  • 2021-10-14
  • 2021-09-10
  • 2021-07-16
  • 2021-11-24
  • 2021-11-26
  • 2022-02-05
  • 2021-09-28
猜你喜欢
  • 2021-12-29
  • 2022-02-03
  • 2021-11-13
  • 2021-07-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案