【Inorder Traversal】

Given a binary tree, return the inorder traversal of its nodes' values.

For example:
Given binary tree {1,#,2,3},

   1
\
2
/
3

return [1,3,2].

Note: Recursive solution is trivial, could you do it iteratively?

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

思路:

preorder用栈两三下就写完了

 vector<int> preorderTraversal(TreeNode *root) {
vector<int> nodes;
if(root == NULL) return nodes;
stack<TreeNode *> tStack;
tStack.push(root);
while(!tStack.empty()){
TreeNode *top = tStack.top();
nodes.push_back(top->val);
tStack.pop();
if(top->right != NULL)
tStack.push(top->right);
if(top->left != NULL)
tStack.push(top->left);
}
return nodes;
}

但是inorder不简单啊,模仿递归记录调用处然后处理完当前函数回来,下午困得不行闷头试了好几次各种bug超Memory,看了我是歌手回来发现脑子清醒了

 vector<int> inorderTraversal(TreeNode *root) {
vector<int> nodes;
if(root == NULL) return nodes;
stack<TreeNode *> tStack;
TreeNode * cur = root;
while(!tStack.empty() || cur != NULL){//假如处理完根的左子树了,tStack也会为空
while(cur != NULL){
tStack.push(cur);//只要当前节点有左孩子,则必须先去访问左子树,而当前节点就得入栈;
cur = cur->left;
}
cur = tStack.top();//如果当前节点为空怎么办?当然就访问它的父节点了,也就是栈顶元素;
tStack.pop();
nodes.push_back(cur->val);//访问完栈顶元素之后就需要将当前节点置为栈顶元素的右孩子
cur = cur->right;
}
return nodes;
}

Postorder与Inorder很相似,但是比Inorder复杂的地方是如何判断该节点的左右子树都已经访问过了,按照Inorder的写法左子树还是先被访问,没有问题,但是访问完左子树后不能直接访问当前节点,要判断当前节点的右子树是否已经被访问,如果没有访问则应该继续去访问右子树,最后再访问当前节点

 vector<int> postorderTraversal(TreeNode *root) {
vector<int> nodes;
if(root == NULL) return nodes;
stack<TreeNode *> tStack;
TreeNode *cur = root; //指向当前要检查的节点
TreeNode *previsited = NULL; //指向前一个被访问的节点
while (!tStack.empty() || cur != NULL) {
while (cur != NULL) { //只要cur有左孩子,则cur入栈,直到cur没有左孩子;
tStack.push(cur);
cur = cur->left;
}
cur = tStack.top();
if (cur->right == NULL || cur->right == previsited) {//当前节点的右孩子如果为空或者已经被访问,则访问当前节点
tStack.pop();//后序遍历访问序列中,当前节点的前驱必然是其右孩子(如果有的话))
nodes.push_back(cur->val);
previsited = cur;
cur = NULL;
}else { //否则访问右孩子,继续上述过程
cur = cur->right;
}
}
return nodes;
}

【题解】【BT】【Leetcode】Binary Tree Preorder/Inorder/Postorder (Iterative Solution)的更多相关文章

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

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

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

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

  3. LC 144. / 94. / 145. Binary Tree Preorder/ Inorder/ PostOrder Traversal

    题目描述 144. Binary Tree Preorder Traversal 94. Binary Tree Inorder Traversal 145. Binary Tree Postorde ...

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

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

  5. leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)

    题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume ...

  6. [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历

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

  7. [leetcode]Binary Tree Preorder Traversal @ Python

    原题地址:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ 题意:这题用递归比较简单.应该考察的是使用非递归实现二叉树的先 ...

  8. [LeetCode] Binary Tree Preorder Traversal

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

  9. LeetCode——Binary Tree Preorder Traversal

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

随机推荐

  1. ACTIVITI 研究代码 之 模版模式

    模板方法模式需要开发抽象类和具体子类的设计师之间的协作.一个设计师负责给出一个算法的轮廓和骨架,另一些设计师则负责给出这个算法的各个逻辑步骤.代表这些具体逻辑步骤的方法称做基本方法(primitive ...

  2. eclipse关联tomcat并且部署java web应用程序

    http://www.ibm.com/developerworks/cn/opensource/os-eclipse-tomcat/

  3. hdu 4632 Palindrome subsequence

    http://acm.hdu.edu.cn/showproblem.php?pid=4632 简单DP 代码: #include<iostream> #include<cstdio& ...

  4. 编码为multipart/form-data自定义类型(包括文件)如何自动绑定到webapi的action的参数里

    application/x-www-form-urlencoded与 multipart/form-data: Fom表单中如果没有type=file的控件,用默认的application/x-www ...

  5. jsoup Cookbook(中文版)--爬虫(java)

    转载:http://www.open-open.com/jsoup/ 目录: 入门 解析和遍历一个html文档 输入 解析一个html字符串 解析一个body片断 根据一个url加载Document对 ...

  6. 【HDU2087】KMP

    KMP算法其实很好理解,就是在匹配串中找最近的相同的串. 下面是HDU的2087: #include<iostream> #include<cstdio> #include&l ...

  7. 数据结构-Stack和Queue

    实现: #include "c2_list.h" template <typename object> class Stack{ public: bool isEmpt ...

  8. OpenLayers简单介绍以及简单实例

    OpenLayers是一个强大的JavaScript包,可以从它的官网免费下载.OpenLayers包含了很多强大的网页地图展示与操作功能,并且能够将不同源的图层展示在同一张地图中,支持各种第三方的地 ...

  9. elementoryOS / ubuntu U盘启动问题的解决

    具体现象: 进入U盘启动后,停顿在"start booting from usb device..."不动. 解决方法:  将syslinux文件夹下的syslinux.cfg中的 ...

  10. iOS开发中XML的DOM和SAX解析方法

    一.介绍 dom是w3c指定的一套规范标准,核心是按树形结构处理数据,dom解析器读入xml文件并在内存中建立一个结构一模一样的“树”,这树各节点和xml各标记对应,通过操纵此“树”来处理xml中的文 ...