题目描述

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. JavaScript--自定义事件Event

    在开发过程中,js原生事件不足以满意开发需求,需要开发者自定义事件. 一.Event Event()构造函数创建一个新的Event. event = new Event(typeArg,eventIn ...

  2. 如何利用awk累加第一列的值?

    以下是一个五行文件的例子: 1.[root@master yjt]# seq 5 |awk 'BEGIN{sum=0;print "总和:"}{if(NR<=4)printf ...

  3. 深入理解JVM——关于垃圾回收

    关于垃圾回收 仿佛来自上海居委会大妈的灵魂拷问:“你是什么垃圾?” 不 今天我们要说的是JVM的垃圾回收 假如我是一个“人”类的“对象”,也和人的生命一样必有一死,可是“我真的还想再活500年~~”, ...

  4. python3编程基础之一:标识符

    每种编程语言都是需要处理数据的,需要变量.函数.类等,而这些都是通过名称访问的.因此,能够作为变量.函数.类等名称的字符串就是标识符.数据,是计算机进行运算的实体.标识符,用来标记的符号,它会指向一个 ...

  5. 初探nodejs事件循环机制event loop

    nodejs的特点 nodejs 具有事件驱动和非阻塞I/O的特点. 事件驱动是指nodejs把每一个任务当成事件来处理. 非阻塞I/O是指nodejs遇到I/O任务时,会从线程池调度单独的线程处理I ...

  6. kafka简介&使用

    架构 几个角色概念 broker 缓存代理,Kafa集群中的一台或多台服务器统称为broker.kafka集群有多个kafka实例组成,每个实例(server)成为broker.每个类型的消息被定义为 ...

  7. Java的反射是什么?有什么用?

    首先我要简单的来说一下什么是Java的反射机制: 在Java里面一个类有两种状态--编译和运行状态,通常我们需要获取这个类的信息都是在编译阶段获得的,也就是直接点出来或者new出来,可是如果需要在类运 ...

  8. object_id()函数

    SQLServer数据库中,如果查询数据库中是否存在指定名称的索引或者外键约束等,经常会用到object_id('name','type')方法,做笔记如下: ? 语法:object_id('obje ...

  9. fidder监控请求响应时间和请求IP

    1.增加监控请求的详情时间 在CustomRules.js的class Handlers中增加  //添加请求的响应时间 public static BindUIColumn("Time T ...

  10. 了解有关 in_memory 工作空间的详细信息

    ArcGIS 提供了一个可写入输出要素类和表的内存工作空间.作为将地理处理输出写入磁盘上的某个位置或网络位置的备选方案,可将输出写入内存工作空间中.通常,将数据写入内存工作空间要明显快于写入其他格式( ...