zoukankan      html  css  js  c++  java
  • [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;
    }
  • 相关阅读:
    update condition 字段报错
    Xshell连接Linux服务器总掉线
    sleep php函数
    ubuntu 16.04 镜像下载
    多线程Parallel和Task
    AngularJS 时间格式化
    正则表达式
    手机抓包
    内存泄漏
    字符集编码和排列规则
  • 原文地址:https://www.cnblogs.com/Azhu/p/4210779.html
Copyright © 2011-2022 走看看