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

[LeetCode] Binary Tree Postorder Traversal dfs,深度搜索的更多相关文章

  1. LeetCode: Binary Tree Postorder Traversal 解题报告

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

  2. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

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

  3. Leetcode Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  4. [Leetcode] Binary tree postorder traversal二叉树后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  5. LeetCode——Binary Tree Postorder Traversal

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

  6. LeetCode: Binary Tree Postorder Traversal [145]

    [题目] Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bi ...

  7. LeetCode Binary Tree Postorder Traversal(数据结构)

    题意: 用迭代法输出一棵二叉树的后序遍历结果. 思路: (1)用两个栈,一个存指针,一个存标记,表示该指针当前已经访问过哪些孩子了. /** * Definition for a binary tre ...

  8. leetcode Binary Tree Postorder Traversal python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  9. leetcode Binary Tree Postorder Traversal 二叉树后续遍历

    先给出递归版本的实现方法,有时间再弄个循环版的.代码如下: /** * Definition for binary tree * struct TreeNode { * int val; * Tree ...

随机推荐

  1. 第四篇:python操作数据库时的传参问题

    python在操作数据库执行sql的时候我们经常会遇到传参问题,以下是我总结的几种方法: 1.格式化字符串 city = 'beijing'cur.execute(“SELECT * FROM %s ...

  2. keepalived原理(主从配置+haproxy)及配置文件详解

    下图描述了使用keepalived+Haproxy主从配置来达到能够针对前段流量进行负载均衡到多台后端web1.web2.web3.img1.img2.但是由于haproxy会存在单点故障问题,因此使 ...

  3. 【Linux】linux 机器之间 zssh, rz, sz互相传输

    服务器端安装lrzsz: sudo  yum install lrzsz 本地客户端安装lrzsz: brew install lrzsz 本地客户端安装zssh: brew install zssh ...

  4. Django框架基础知识01-配置环境

    Django框架 Django是个怎样的东西呢? Web应用框架----Django  http服务器:用来接受用户请求,并将请求转发给web应用框架进行处理. Web应用框架处理完以后再发送给htt ...

  5. A1095 Cars on Campus (30)(30 分)

    A1095 Cars on Campus (30)(30 分) Zhejiang University has 6 campuses and a lot of gates. From each gat ...

  6. easyui-numberbox限定仅输入数字

    许多必填项都涉及到数字,比如电话号码,身份证号这些要求用户在输入时只能输入数字.Easyui提供了数字框控件,允许用户只输入数字, <td> <input id="ssd& ...

  7. 67、activity中调用fragment内部自定义的方法

    fragment: /** * author: Created by zzl on 15/11/19. */ @SuppressLint("validFragment") publ ...

  8. MongoDB快速入门学习笔记6 MongoDB的文档删除操作

    db.集合名称.remove({query}, justOne)query:过滤条件,可选justOne:是否只删除查询到的第一条数据,值为true或者1时,只删除一条数据,默认为false,可选. ...

  9. python - 接口自动化测试 - HttpRequest - 接口测试类封装

    # -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: http_request.py @ide: PyCharm ...

  10. 如何解决Jmeter导出的聚合报告是乱码易位问题

    在使用Jmeter这个工具的时候,有些单词不懂是什么意思,就切换到这个工具自带的中文语言: 当我们测试完毕,导出聚合报告(Summary Report)的时候: 1.有一些第一个Title下面的中文是 ...