1. Binary Tree Postorder Traversal My Submissions QuestionEditorial Solution

    Total Accepted: 97358 Total Submissions: 273744 Difficulty: Hard

    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?

二叉树的后续遍历,不使用递归实现

思路:类似于中序,需要额外使用一个map来保存相应的访问转态

/**
* Definition for a binary tree node.
* 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;
if(root==NULL)return res;
stack<TreeNode*> st;
st.push(root);
TreeNode * rn=root;
map<TreeNode*,int> map;
map[rn]=1;
while(!st.empty()){
while(rn!=NULL&&rn->left!=NULL&&!map.count(rn->left)){//走到最左下角
st.push(rn->left);
rn = rn->left;
map[rn]=1;
}
if(rn->right!=NULL&&!map.count(rn->right)){ //右子树不空进入右子树
rn = rn->right;
st.push(rn);
map[rn]=1;
}
else { //为空则访问该节点
res.push_back(rn->val);
st.pop();
if(!st.empty())rn = st.top(); //回溯到上一个节点
}
}
return res;
}
};

68-Binary Tree Postorder Traversal的更多相关文章

  1. 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal

    详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal            Given a binary tree, return the po ...

  2. Binary Tree Preorder Traversal and Binary Tree Postorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

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

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

  4. LeetCode: Binary Tree Postorder Traversal 解题报告

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

  5. 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

  6. 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator

    144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

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

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

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

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

  9. 145. Binary Tree Postorder Traversal

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

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

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

随机推荐

  1. [对对子队]会议记录4.18(Scrum Meeting9)

    今天已完成的工作 何瑞 ​ 工作内容:修复了一些关卡1的bug ​ 相关issue:搭建关卡1 ​ 相关签入:4.18签入1 4.18签入2 梁河览 ​ 工作内容:实现了音量控制,添加了BGM ​ 相 ...

  2. 2021.9.17考试总结[NOIP模拟55]

    有的考试表面上自称NOIP模拟,背地里却是绍兴一中NOI模拟 吓得我直接文件打错 T1 Skip 设状态$f_i$为最后一次选$i$在$i$时的最优解.有$f_i=max_{j<i}[f_j+a ...

  3. Noip模拟15 2021.7.14

    T1 夜莺与玫瑰 题目越发的变态起来... 这题刚开始看超级像仪仗队,好不容易码完欧拉函数后尝试×2后输出但不对!! 于是选择了跳过.... 正解居然是莫比乌斯函数....我也是醉了 预处理完就剩下$ ...

  4. Spring Security:Authentication 认证(一)

    1. Spring Security 简介 在 Spring 生态系统中,为他的项目增加安全性,你可以借助 Spring Security 库来做到这一点. 那什么是 Spring Security? ...

  5. Educational Codeforces Round 114 (Rated for Div. 2)题解

    还是常规的过了A,B,C还是在D上卡了... D. The Strongest Build 简化题意:给定你n组东西,每组东西都有\(c_i\)个装备,每个装备有一个武力值\(a_{i,j}\),要求 ...

  6. switch中case...用法-c语言

    ... 表示范围 case 0...4;   // error case 5 ... 9; // ok eg 1: char ch = 4; switch(ch) { case 1: printf(& ...

  7. UVA1104 Chips Challenge

    一.题目 有一个 \(n\times n\) 的矩阵,每个元素可能是 ..C./ 的其中一种,分别表示可以放置芯片.已经放置了芯片.不能放置芯片,你可以分别决定是否可以放置芯片的位置放置芯片. 最后需 ...

  8. SpringCloud升级之路2020.0.x版-29.Spring Cloud OpenFeign 的解析(1)

    本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 在使用云原生的很多微服务中,比较小规模的可能直接依靠云服务中的负载均衡器进行内部域名与服务 ...

  9. jmeter 数据库压力测试之MySql

    1.首先下载合适的数据库驱动:https://mvnrepository.com/artifact/mysql/mysql-connector-java 2.创建testplan,并添加jar包 3. ...

  10. Java后台常用方法(更新中)

    String字符串 API文档地址:中文 英文 String类在java.lang包中,java使用String类创建字符串变量,字符串变量属于对象. String类对象创建后不能修改,String变 ...