前往二叉树的:前序,中序,后序 遍历算法

方法一:递归

    vector<int> res;
vector<int> postorderTraversal(TreeNode* root) {
if (!root) return res;
if (root->left) postorderTraversal(root->left);
if (root->right) postorderTraversal(root->right);
res.push_back(root->val);
return res;
}

方法二:非递归

    vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
if (!root) return res;
stack<TreeNode*> S;
TreeNode* p=root, *r=nullptr;
while (p||!S.empty())
{
if (p)
{
S.push(p);
p=p->left;
}
else
{
p=S.top();
if (p->right&&p->right!=r)
p=p->right;
else
{
S.pop();
res.push_back(p->val);
r=p;
p=nullptr;
}
}
}
return res;
}

方法三:非递归

    vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
if (!root) return res;
stack<TreeNode*> S;
TreeNode* p=root;
S.push(p);
while (!S.empty())
{
p=S.top();
S.pop();
if (p->left) S.push(p->left);
if (p->right) S.push(p->right);
res.insert(res.begin(),p->val);
}
return res;
}

【leetcode 145. 二叉树的后序遍历】解题报告的更多相关文章

  1. LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)

    145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...

  2. Java实现 LeetCode 145 二叉树的后序遍历

    145. 二叉树的后序遍历 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成 ...

  3. LeetCode 145 二叉树的后序遍历(非递归)

    题目: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路: 1 ...

  4. LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)

    题目描述 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 后 ...

  5. Leetcode 145. 二叉树的后序遍历

    题目链接 https://leetcode-cn.com/problems/binary-tree-postorder-traversal/description/ 题目描述 给定一个二叉树,返回它的 ...

  6. LeetCode 145. 二叉树的后序遍历 (用栈实现后序遍历二叉树的非递归算法)

    题目链接:https://leetcode-cn.com/problems/binary-tree-postorder-traversal/ 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [ ...

  7. LeetCode 145 ——二叉树的后序遍历

    1. 题目 2. 解答 2.1. 递归法 定义一个存放树中数据的向量 data,从根节点开始,如果节点不为空,那么 递归得到其左子树的数据向量 temp,将 temp 合并到 data 中去 递归得到 ...

  8. LeetCode:二叉树的后序遍历【145】

    LeetCode:二叉树的后序遍历[145] 题目描述 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...

  9. 【LeetCode】145. 二叉树的后序遍历

    145. 二叉树的后序遍历 知识点:二叉树:递归:Morris遍历 题目描述 给定一个二叉树的根节点 root ,返回它的 后序 遍历. 示例 输入: [1,null,2,3] 1 \ 2 / 3 输 ...

随机推荐

  1. 子查询语句的thinkphp实现

    语句 SELECT a.id as item_id,a.name as item_name,a.intro as item_intro,b.id,b.money FROM sh_incentive_i ...

  2. php无刷新上传图片

    1. 引入文件 <!--图片上传begin--> <script type="text/javascript" src="/js/jquery.form ...

  3. PL/SQL 训练09--面向对象

    ---对象基本声明.实现.使用--对象类型,类似与JAVA中的类,通俗的讲,就是捆绑了相关函数和过程的记录类型. ---对象声明 --create type 创建一个对象类型的规范部分 create ...

  4. python开发函数进阶:内置函数

    一,内置函数 #内置的模块#拿过来直接就用的:内置的函数 #!/usr/bin/env python #_*_coding:utf-8_*_ #内置的模块 #拿过来直接就用的:内置的函数 #作用域相关 ...

  5. ubuntu 14.04使用root登陆出现错误“Error found when loading /root/.profile”解决

    在刚修改完root权限自动登录后,发现开机出现以下提示: Error found when loading /root/.profile stdin:is not a tty ----........ ...

  6. spring+hibernate ---含AOP--事务--laobai

    biz包: package com.etc.biz; import java.util.List; import org.springframework.orm.hibernate3.support. ...

  7. Oracle 11G的间隔(INTERVAL)分区

    -- Create table create table MS_BIGTABLE_LOG ( record_date DATE, col_1 VARCHAR2(), col_2 VARCHAR2() ...

  8. Shell编程的基本语法

    Shell编程 创建sh文件 touch test.sh vim test.sh 写入如下内容 #!/bin/bash a="hello" 运行 chmod +x /root/te ...

  9. collections、time和datetime模块

    主要内容: 一.collections模块 二.time模块 三.datetime模块 1️⃣  collection模块 1.什么是collections模块.干什么用? collections模块 ...

  10. php 读取和下载execl

    最近用到php 对excel 的操作 下来 小弟为大家 先贴一下自己的代码  有什么补充的 大神们请指点下.感激不尽. 我用的是yii2   yii2中有类 phpexcel 先说说下载吧. 首先下载 ...