145. Binary Tree Postorder Traversal

Total Accepted: 96378 Total Submissions: 271797 Difficulty: Hard


提交网址: https://leetcode.com/problems/binary-tree-postorder-traversal/

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?

分析:

AC代码:

#include<iostream>
#include<vector>
#include<stack>
#include<algorithm> // 引入 reverse函数,对vector进行反转
using namespace std; 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> res(0);
if(root==NULL) return res;
stack<TreeNode *> st;
TreeNode *p=root;
while(!st.empty()|| p!=NULL)
{
if(p!=NULL)
{
st.push(p);
res.push_back(p->val);
p=p->right;
}
if(p==NULL)
{
p=st.top();
p=p->left;
st.pop();
}
}
reverse(res.begin(),res.end()); // 对向量及其内部结构进行反转
return res;
}
};
// 以下为测试
int main()
{
Solution sol;
vector<int> res; TreeNode *root = new TreeNode(1);
root->right = new TreeNode(2);
root->right->left = new TreeNode(3); res=sol.postorderTraversal(root); for(int i:res)
cout<<i<<" "; // 此处为vector遍历的方法,C++11标准支持
return 0;
}

另一解法(非递归):

后序遍历的麻烦在于访问过右子树之后,第二次访问的时候就必须访问根节点,而不是继续访问右子树,所以使用pre来记录上次访问的节点...

class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
vector<int> ans;
if(root == NULL) return ans;
stack<TreeNode* > s;
TreeNode * p = root;
TreeNode * pre = NULL; // 记录上次访问的节点
while(p != NULL || !s.empty()) {
while(p != NULL) {
s.push(p);
p = p->left;
}
if(!s.empty()) {
p = s.top();
s.pop();
if(p->right == NULL || p->right == pre) { // 右子树为空,或者是访问过
ans.push_back(p->val);
pre = p; // 记录刚刚访问的右节点
p = NULL;
}
else {
s.push(p);
p = p->right;
}
}
}
return ans;
}
};

C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)的更多相关文章

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

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

  2. LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...

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

    题目: Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,nul ...

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

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

  5. leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

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

  6. 145 Binary Tree Postorder Traversal 二叉树的后序遍历

    给定一棵二叉树,返回其节点值的后序遍历.例如:给定二叉树 [1,null,2,3],   1    \     2    /   3返回 [3,2,1].注意: 递归方法很简单,你可以使用迭代方法来解 ...

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

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

  8. lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历

    题目: 二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] 挑战 你能使用非递归实现么? 解题: 递归程序 ...

  9. Leetcode145. Binary Tree Postorder Traversal二叉树的后序遍历

    给定一个二叉树,返回它的 后序 遍历. 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class Solution { public: vector<int> res; ve ...

随机推荐

  1. get windows auth code

    public static WindowsIdentityInfo GetWindowsIdentityInfo(HttpContext context) { WindowsIdentityInfo ...

  2. TypeError: 'range' object does not support item assignment处理方法

    vectorsum.py#!/usr/bin/env/pythonimport sysfrom datetime import datetimeimport numpy as np # def num ...

  3. 配置了yum本地源

    测试机不能联网  所以不能直接安装软件  只能配置本地源 1.   在联网的电脑上下载与Linux内核版本相同的镜像 2.   把此安装镜像放在此Linux测试机上  比如放在家目录下  /home/ ...

  4. 关于管理,你可能一直有 3 个误解zz

    很多管理者认为,下属绩效低是由于其能力不行.其实,下属的绩效是由管理者决定的.一个好的管理者,必须对管理有正确的认知,才能形成有效的管理行为,让下属拥有绩效,并获得成长.来源丨春暖花开(ID:CCH_ ...

  5. 2019.03.26 bzoj4446: [Scoi2015]小凸玩密室(树形dp)

    传送门 题意简述: 给一棵完全二叉树,有点权aia_iai​和边权,每个点有一盏灯,现在要按一定要求点亮: 任意时刻点亮的灯泡必须连通 点亮一个灯泡后必须先点亮其子树 费用计算如下:点第一盏灯不要花费 ...

  6. Windows7+IIS+PHP7+MySQL5.7环境搭建

    IIS配置 本次搭建使用的系统是Windows7,Windows8,10与此类似. 开启IIS 开始-->控制面板-->程序和功能,点击左边栏的开启或关闭Windows功能,如图: 选择I ...

  7. Chrome 的 PNaCl 还活着么?

    WebAssembly Migration Guide Given the momentum of cross-browser WebAssembly support, we plan to focu ...

  8. 网络操作系统 第九章 DHCP服务器管理与配置

    本章小结 本章介绍了DHCP服务器的基本概念,基本原理和主要功能,详细说明了Window是下DHCP服务器的安装配置和Linux下DHCP 服务器的安装配置,通过本章的学习.读者能够理解动态主机配置协 ...

  9. BABOK概述

    BABOK概述 BABOK(Business Analysis Body of Knowlodge: 业务[或商业]分析知识体系)是由位于加拿大的IIBA(International Institut ...

  10. MySQL基础知识3