题目描述

144. Binary Tree Preorder Traversal
94. Binary Tree Inorder Traversal
145. Binary Tree Postorder Traversal

前序排列 :根-左-右

中序排列: 左-根-右

后序排列:左-右-根

参考答案

 // PreOrder
/**
* 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 {
private:
vector<int> res; public:
vector<int> preorderTraversal(TreeNode* root) {
foo(root);
return res;
}
void foo(TreeNode* root){
if(root == NULL)
return; res.push_back(root->val);
foo(root->left);
foo(root->right);
}
}; // InOrder
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
if(root == NULL){
return vector<int>();
}
vector<int> result; // 创建结果
stack<TreeNode *> s;// 创建堆栈
TreeNode *p = root; // 临时让p为root
// 寻找p最左边的node
while(p!=NULL){
s.push(p); // 从root开始,将左边的node推入stack
p = p->left;// 更新p为左node
}
// s 为全是左节点的stack
// 对 s进行循环操作
while(!s.empty()){
// 将最最左边的,推入stack
p = s.top();
result.push_back(p->val);
s.pop(); // 自己消失了
// 然后观察这个的右边node
if(p->right != NULL){
p = p->right;
while(p!=NULL){ //观察node的左边
s.push(p);
p = p->left;
}
}
}
return result;
}
}; // PostOrder class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
if(root == NULL){
return vector<int>();
}
vector<int> result;
stack<TreeNode *> s; s.push(root);
while(!s.empty()){
TreeNode *temp = s.top();
result.push_back(temp->val);
s.pop();
if(temp->left!=NULL){
s.push(temp->left);
}
if(temp->right!= NULL){
s.push(temp->right);
}
}
reverse(result.begin(), result.end());
return result; }
};
 

LC 144. / 94. / 145. Binary Tree Preorder/ Inorder/ PostOrder Traversal的更多相关文章

  1. LeetCode之“树”:Binary Tree Preorder && Inorder && Postorder Traversal

    Binary Tree Preorder Traversal 题目链接 题目要求: Given a binary tree, return the preorder traversal of its ...

  2. [LeetCode] Binary Tree Preorder/Inorder/Postorder Traversal

    前中后遍历 递归版 /* Recursive solution */ class Solution { public: vector<int> preorderTraversal(Tree ...

  3. 【题解】【BT】【Leetcode】Binary Tree Preorder/Inorder/Postorder (Iterative Solution)

    [Inorder Traversal] Given a binary tree, return the inorder traversal of its nodes' values. For exam ...

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

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

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

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

  6. 145. Binary Tree Postorder Traversal

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

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

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

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

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

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

随机推荐

  1. vue的ui组件库

    https://www.cnblogs.com/dupd/p/7735450.html

  2. bzoj2406 矩阵

    我们不妨想一想,这道题目又有\(abs\)又有\(Max\)不是很好算对吧. 所以我们二分答案,考虑怎么\(check\). 对于一个点,显然它能够取的范围是\([l,r]\),接着是对于一行一列都有 ...

  3. IO流——常用IO流详解

    1:字节流 字节流:用于处理以字节为单位的二进制文件(如音乐,图片等) InputStream 是抽象类 它的对应子类FileInputStream可以被实例化 构造方法: FileInputStre ...

  4. AOP 与 Spring中AOP使用(下)

    AOP通知类型 前置通知 在目标方法执行之前进行操作 UserDao.java public class UserDao { public void add(){ System.out.println ...

  5. 关于Kernel的思考

    学习播客_KLDA(推导得很通俗,下面的推导就是源于此篇博客) 第一部分:按照自己的理解,模仿抄!学习播客来完成一下KLDA的推导. 第二部分:对于Kernel的思考 KLDA:顾名思义,就是把Ker ...

  6. Cesium Workshop

    参考资料: https://cesiumjs.org/tutorials/Cesium-Workshop/ https://github.com/geoadmin/workshop-cesium3d ...

  7. 【译】Solr in Action 第二章

    2.1 2.2 2.3 基本废话 2.4 基本废话

  8. uboot自定义添加命令

    1.添加命令 1.u-boot的命令格式: U_BOOT_CMD(name,maxargs,repeatable,command,”usage”,"help") name:命令的名 ...

  9. 环境初始化 Build and Install the Apache Thrift IDL Compiler Install the Platform Development Tools

    Apache Thrift - Centos 6.5 Install http://thrift.apache.org/docs/install/centos Building Apache Thri ...

  10. sql中union,union all没有兼顾到的内容

    今日遇到一个问题,两张表联合取交集去重,但是需要把某一字段相同的也给去掉 union all : 联合,没有取交集 union :联合取交集(仅针对所有字段相同的去重) 解决方案:将联合的数据作为一个 ...