后序遍历,比先序和中序都要复杂。访问一个结点前,需要先判断其右孩子是否被访问过。如果是,则可以访问该结点;否则,需要先处理右子树。

 vector<int> postorderTraversal(TreeNode *root)
{
vector<int> result;
stack<TreeNode *>s;
TreeNode *p, *q;//一个表示当前访问的结点,一个表示刚刚访问过的结点
p = root;
do
{
while (p != nullptr)
{
//不断将左结点压入
s.push(p);
p = p->left;
}
q = nullptr;//压到底时,刚刚访问过的结点必定为空结点
while (!s.empty())
{
p = s.top();
s.pop();
if (p->right == q)
{
//如果当前结点的右结点已经被访问,则访问该结点
result.push_back(p->val);
q = p;
}
else
{
//右孩子没有被访问过,则继续压入,先处理右子树
s.push(p);
p = p->right;
break;
}
}
} while (!s.empty());
}

Leetcode 之Binary Tree Postorder Traversal(44)的更多相关文章

  1. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

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

  2. C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)

    145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...

  3. LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)

    翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...

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

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

  5. Java for LeetCode 145 Binary Tree Postorder Traversal

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

  6. leetcode 145. Binary Tree Postorder Traversal ----- java

    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 bina ...

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

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

  9. leetcode - [6]Binary Tree Postorder Traversal

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

  10. 【leetcode】Binary Tree Postorder Traversal

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

随机推荐

  1. 【POJ3621】【洛谷2868】Sightseeing Cows(分数规划)

    [POJ3621][洛谷2868]Sightseeing Cows(分数规划) 题面 Vjudge 洛谷 大意: 在有向图图中选出一个环,使得这个环的点权\(/\)边权最大 题解 分数规划 二分答案之 ...

  2. BZOJ5312:冒险——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=5312 Kaiser终于成为冒险协会的一员,这次冒险协会派他去冒险,他来到一处古墓,却被大门上的守护 ...

  3. 那些常用的JS命令

    window.location.reload()刷新当前页面. parent.location.reload()刷新父亲对象(用于框架) opener.location.reload()刷新父窗口对象 ...

  4. selenium - webdriver - Keys类(键盘操作)

    Keys()类提供了键盘上几乎所有按键的方法,这个类可用来模拟键盘上的按键,包括各种组合键,如 Ctrl+A, Ctrl+X,Ctrl+C, Ctrl+V 等等 from selenium impor ...

  5. 第01篇 为什么推荐使用String直接赋值

    在四海学的时候,可能需要我们经过沉淀才会去想一些事情,有的时候不知道为什么这样或者那样的时候,从今天看是,胖先生打算给大家开辟一个课程,就是我的读书笔记. 首先我们来认识一下String字符串 一般对 ...

  6. Java中x=x+1 与x+=1 的一点区别

    转载自:http://www.cnblogs.com/heshan664754022/archive/2013/04/01/2994028.html 作者:十年半山 今天同悦姐学到了关于Java的复合 ...

  7. Spring实战第一部分总结

                                                         Spring实战第一部分总结 第一章 综述 1. DI依赖注入让相互协作的组件保持松散耦合,而 ...

  8. 不使用Tomcat,手写简单的web服务

    背景: 公司使用的YDB提供了http的查询数据库服务,直接通过url传入sql语句查询数据-_-||.ydb的使用参照:https://www.cnblogs.com/hd-zg/p/7115112 ...

  9. Could not load file or assembly 'Microsoft.ReportViewer.Common, Version=11.0.0.0 异常处理

    在本机开发asp.net .rdlc报表后,部署到本地没有问题. 当把网站发布后部署在IIS上,新电脑上(只安装了.net framwork4.5),提示如下错误: “Could not load f ...

  10. 【bzoj3362-导航难题】带权并查集

    题意: 约翰所在的乡村可以看做一个二维平面,其中有N 座牧场,每座牧场都有自己的坐标,编号为1到N.牧场间存在一些道路,每条道路道路连接两个不同的牧场,方向必定平行于X 轴或Y轴.连通两座牧场之间的路 ...