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. setInterval与setTimeout

    在自己用canvas画一个时钟时,画秒钟用的是利用图片将重复的线条遮住,但是会出现有两个秒钟线条同时存在,才想起setInterval有那么个坑,查了点资料,记录下,若有不对的或者未写到的点,还请大家 ...

  2. Python知识点入门笔记——Python文件操作、异常处理及random模块使用

    文件是存储在外部介质的数据集合,通常可以长久保存,前提是介质不易损坏 Python的绝对路径写法: E:\\编程学习资料\\爬取某社区高清无码大图.py E:/编程学习资料/爬取某社区高清无码大图.p ...

  3. 爬虫之Scarpy.Request

    一 .Request 1.request Scarpy中的HTTP请求对象 1.1.Requse的构造 #我们ctrl+左键可以看到Scarpy.Request的代码 class Request(ob ...

  4. 20190102(多线程,守护线程,线程互斥锁,信号量,JoinableQueue)

    多线程 多进程: 核心是多道技术,本质上就是切换加保存技术. 当进程IO操作较多,可以提高程序效率. 每个进程都默认有一条主线程. 多线程: 程序的执行线路,相当于一条流水线,其包含了程序的具体执行步 ...

  5. Diycode开源项目 NotificationActivity

    1.NotificationActivity预览以及布局详解 1.1.首先看一下通知的具体页面. 1.2.然后是布局代码==>activity_fragment.xml <LinearLa ...

  6. [项目2] 10mins

    1.准备工作 M层:生成虚假数据 from django.db import models from faker import Factory # Create your models here. c ...

  7. Azure Active Directory Connect是如何协助管理员工作的?

    TechTarget中国原创] 应用基于云的Microsoft Azure Active Directory,管理员们可以将本地Active Directory整合到Windows Server中.通 ...

  8. 安装 Redis的Python客户端redis-py

    安装 redis-py 使用easy_install sudo easy_install redis 源码安装 git clone https://github.com/andymccurdy/red ...

  9. Goole Search Auto Complete

    这个项目就九章算法大数据课程的一个项目.主要分为两步: 第一步是 offline 建立 数据库 我们用两个map reduce 的data pipline 来实现. 第二步是 online显示把数据里 ...

  10. MCMC 浅谈

    # MCMC 浅谈 1. 采样(sampling)是什么 MCMC在采样算法中有着举足轻重的地位,那么什么是采样?采样就是根据某种分布生成样本.举个例子,线性同余发生器就是根据均匀分布生成样本,这就很 ...