【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. EF Code First:实体映射,数据迁移,重构(1)

    一.前言 经过EF的<第一篇>,我们已经把数据访问层基本搭建起来了,但并没有涉及实体关系.实体关系对于一个数据库系统来说至关重要,而且EF的各个实体之间的联系,实体之间的协作,联合查询等也 ...

  2. struts2 拦截器 interceptor

    struts2 拦截器详解:http://struts2.group.iteye.com/group/wiki/1397-deep-into-struts2-interceptors

  3. Sharepoint2010突然之间不能打开页面,报503错误The service is unavailable

    原因:安装Sahrepoint时的账号出现故障,可能是密码过期等等. 解决方案: 新建windows用户ada,密码设置为永不过期,隶属于:administrators/IIS-WPG/WSS-WPG ...

  4. Asynchttpclient开源框架下载图片和文本,于Volley和Glide开源框架的区别。

    AsyncHttpClient是一款比较流行的Android异步网路加载库,在github上的网址是:https://github.com/loopj/android-async-httpAsyncH ...

  5. 闭包 (循环事件获取不到i) 和 各种解决循环获取不到i的解决方法

    for(var i in fav){ (function(){                var p=i;                var obj=$S.getId(fav[i]);     ...

  6. Jquery API Hybrid APP调研

    http://jquery.cuishifeng.cn/source.html   hybrid app Hybrid App(混合模式移动应用)是指介于web-app.native-app这两者之间 ...

  7. py零散知识点

    变量之间的赋值是公用一个地址比如 a = 3 b = a b和a用的是一个地址 在Python中 b = a.copy() a和b就不是一个地址了 -------------------------- ...

  8. wp8.1 Study10:APP数据存储

    一.理论 1.App的各种数据在WP哪里的? 下图很好介绍了这个问题.有InstalltionFolder, knownFolder, SD Card... 2.一个App的数据存储概览 主要分两大部 ...

  9. 【转发】linux yum命令详解

    linux yum命令详解 yum(全 称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器.基於RPM包管理, ...

  10. JVM-内存分配与回收策略

    简单介绍一下Java技术体系下的Java虚拟机内存分配与回收策略.  1.对象优先在Eden分配  大多数情况下,对象在新生代Eden区中分分配.当Eden区已没有足够空间进行分配时,虚拟机将发起一次 ...