Binary Tree Postorder

Given a binary tree, return the postorder traversal of its nodes’ values.

For example:

Given binary tree {1,#,2,3},return [3,2,1].

Note: Recursive solution is trivial, could you do it iteratively?

这是一道LeetCode中标记为Hard的题。事实上如果没有限定不使用递归的话,这道题是非常简单的。所以我只简单回顾一下这道题的两种解法:递归和迭代。

递归法实现后序遍历

算法复杂度为O(n)

class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> re;
print(root,re);
return re;
}
void print(TreeNode *node,vector<int> &re){
if(node == NULL) return;
print(node->left,re);//左
print(node->right,re);//右
re.push_back(node->val);//中
}
};

递归实现前序遍历和后序遍历,只要把print函数中“左右中”三行代码改成相应的顺序即可。

迭代实现后序遍历

迭代实现遍历的本质是广度优先搜索,思路如下:

  • Create an empty stack, Push root node to the stack.
  • Do following while stack is not empty.
  • pop an item from the stack and print it.
  • push the left child of popped item to stack.
  • push the right child of popped item to stack.
  • reverse the ouput.

其中,容易搞错的是输出“中”后,要先push左节点,再push右节点。因为对栈来说,先进去的左节点会后输出(先进后出,后进先出),就实现了“中右左”的顺序,再反转(reverse)就得到了后续遍历(左右中)。

算法复杂度为O(n)

class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> re;
stack<TreeNode*> visit;
if(root != NULL)
visit.push(root);
while(!visit.empty()){
TreeNode *topNode = visit.top();
visit.pop();//top方法只是获取最上面的元素,所以要用pop方法弹出
re.push_back(topNode->val);
if(topNode->left != NULL)
visit.push(topNode->left);
if(topNode->right != NULL)
visit.push(topNode->right);
}
reverse(re.begin(),re.end());
return re;
}
};

[LeetCode] Binary Tree Postorder题解的更多相关文章

  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 Transversal

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

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

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

  6. [LeetCode] Binary Tree Postorder Traversal dfs,深度搜索

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

  7. LeetCode——Binary Tree Postorder Traversal

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

  8. LeetCode: Binary Tree Postorder Traversal [145]

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

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

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

随机推荐

  1. blueborne漏洞的联想

    本文作者:ice 0X00前言 昨天看到blueborne的漏洞,顺手给我的nexus6装了一个app,测试了一下,一脸懵逼,怎么修复啊,然后我联想了一下, 还有哪些协议和传输是我们身边的威胁了,于是 ...

  2. nginx实现动静分离--附nginx配置文件详解

    转自http://www.cnblogs.com/1214804270hacker/p/9299462.html 一.认识访问静态资源与访问动态资源的区别 静态资源:指存储在硬盘内的数据,固定的数据, ...

  3. Jmeter使用吞吐量控制器实现不同的用户操纵不同的业务

    一.需求 需求:博客系统,模拟用户真实行为,80%的用户阅读文章,20%的用户创建文章,创建文章的用户随机的删除或者修改文章. 二.脚本实现 80%的用户查看文章 20%用户创建文章 根据post_i ...

  4. Vue 父子组件传递方式

    问题: parent.vue <template> <div> 父组件 <child :childObject="asyncObject">&l ...

  5. spring属性配置执行过程,单列和原型区别

    Spring配置中,采用属性注入时,当创建IOC容器时,也直接创建对象,并且执行相对应的setter方法 Student.java package com.scope; public class St ...

  6. 关于window.onload和body onload冲突的解决办法

    在学习用js在 页面中动态显示当前时间 和依次读取公告栏信息的 实验中 发现在将两个页面整合时 window.onload=function (){}和 <body onload="d ...

  7. phpspreadsheet开发手记

    坑安装简单示例通过模板来生成文件释放内存单元格根据索引获取英文列设置值合并单元格居中显示宽度设置批量设置单元格格式直接输出下载自动计算列宽函数formula单元格变可点击的超链 PhpSpreadsh ...

  8. Mac下使用Wine安装正则表达式工具RegexBuddy 4

    下载: (链接: https://pan.baidu.com/s/1bzRae6 密码: b5d3) 安装: 1.安装Wine 参考:http://www.cnblogs.com/EasonJim/p ...

  9. 不好意思啊,我上周到今天不到10天时间,用纯C语言写了一个小站!想拍砖的就赶紧拿出来拍啊

    花10天时间用C语言做了个小站 http://tieba.yunxunmi.com/index.html 简称: 云贴吧 不好意思啊,我上周到今天不到10天时间,用纯C语言写了一个小站!想拍砖的就赶紧 ...

  10. DB2 命令大全

    check Archiving processing 查看日志归档情况 db2 "SELECT DATE(CAST(START_TIME as TIMESTAMP)) as DATE, co ...