Binary Tree Inorder Traversal-非递归实现中序遍历二叉树
题目描述:
给定一颗二叉树,使用非递归方法实现二叉树的中序遍历
- 题目来源:
- http://oj.leetcode.com/problems/binary-tree-inorder-traversal/
- 题目分析:
- 递归到非递归的转换。使用栈描述递归的调用过程,while循环体计算递归程序的计算部分。因为每次while循环只能处理一次递归调用,使用标记记录栈中节点的计算痕迹,例如:用tag记录当前根的调用记录,当根的左右子树均未调用时,令tag值为0,当根的左子树已经调用过时,令tag值为1。
- 时间复杂度:O(n) n为节点数
- 示例代码:
vector<int> inorderTraversal(TreeNode *root) {
stack<TreeNode*> stnode;
stack<char> sttag;
vector<int> result; if(root == NULL)
return result; stnode.push(root);
sttag.push('');
while(!stnode.empty()) {
TreeNode* topnode = stnode.top();
char toptag = sttag.top();
if(toptag == '') {
sttag.pop();
sttag.push('');
if(topnode->left != NULL) {
stnode.push(topnode->left);
sttag.push('');
}
} else if(toptag == '') {
result.push_back(topnode->val);
stnode.pop();
sttag.pop();
if(topnode->right != NULL) {
stnode.push(topnode->right);
sttag.push('');
}
}
} return result;
}
Binary Tree Inorder Traversal-非递归实现中序遍历二叉树的更多相关文章
- leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume t ...
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 105 Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树
给定一棵树的前序遍历与中序遍历,依据此构造二叉树.注意:你可以假设树中没有重复的元素.例如,给出前序遍历 = [3,9,20,15,7]中序遍历 = [9,3,15,20,7]返回如下的二叉树: ...
- LeetCode:105_Construct Binary Tree from Preorder and Inorder Traversal | 根据前序和中序遍历构建二叉树 | Medium
要求:通过二叉树的前序和中序遍历序列构建一颗二叉树 代码如下: struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode ...
- leetcode 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)
题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...
- leetCode 94.Binary Tree Inorder Traversal(二叉树中序遍历) 解题思路和方法
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
- LeetCode 94. Binary Tree Inorder Traversal 动态演示
非递归的中序遍历,要用到一个stack class Solution { public: vector<int> inorderTraversal(TreeNode* root) { ve ...
- 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
随机推荐
- Android中处理崩溃异常和记录日志(转)
现在安装Android系统的手机版本和设备千差万别,在模拟器上运行良好的程序安装到某款手机上说不定就出现崩溃的现象,开发者个人不可能购买所有设备逐个调试,所以在程序发布出去之后,如果出现了崩溃现象,开 ...
- andeoid硬件解码
Finally, I must say, finally, we get low-level media APIs in Android, the Android hardware decoding ...
- Linux 进程状态 说明
Linux是一个多用户,多任务的系统,可以同时运行多个用户的多个程序,就必然会产生很多的进程,而每个进程会有不同的状态. 在下文将对进程的 R.S.D.T.Z.X 六种状态做个说明. 进程状态: S ...
- Error -27728: Step download timeout (120 seconds)的解决方法(转)
LR中超时问题解决方法 超时错误在LoadRunner录制Web协议脚本回放时超时经常出现. 现象1:Action.c(16): Error -27728: Step download timeout ...
- 【JAVA学习】struts2的action中使用session的方法
尊重版权:http://hi.baidu.com/dillisbest/item/0bdc35c0b477b853ad00efac 在Struts2里,假设须要在Action中使用session.能够 ...
- 流畅python学习笔记:第十六章:协程
通常在python进行编程一般都是使用多线程或者多进程来实现.这里介绍另外一种并发的方式,就是协程,但和多线程以及多进程不一样的是,协程是运行在单线程当中的并发.来看下具体的例子: def simpl ...
- matlab使用usb和gige 网口相机
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 辛苦原创所得,转载请注明出处 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ...
- Android tab页制作
全屏启动页 activity_launcher <?xml version="1.0" encoding="utf-8"?> <Relativ ...
- 使用expect实现shell自动交互
expect 是一个自动交互功能的工具.expect 是开了一个子进程,通过 spawn 来执行 shell 脚本,监测到脚本的返回结果,通过 expect 判断要进行的交互输入内容. expect ...
- java多线程系列笔记 目录
基础篇 Java多线程系列 基础篇01 线程的状态 Java多线程系列 基础篇02 线程的创建和运行 Java多线程系列 基础篇03 线程的优先级和守护线程 Java多线程系列 基础篇04 线程中断 ...