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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
随机推荐
- Nginx 经验小结
chmod 777 永远不要 使用 777,有时候可以懒惰的解决权限问题, 但是它同样也表示你没有线索去解决权限问题,你只是在碰运气. 你应该检查整个路径的权限,并思考发生了什么事情. 把 root ...
- 11 redis之rdb快照持久化
一:Redis持久化配置 Redis的持久化有2种方式[快照,是日志] 二:Rdb快照的配置选项 save 900 1 // 900内,有1条写入,则产生快照 save 300 1000 // 如果3 ...
- 17 nginx连接memcached
一:配置php扩展memcached wget http://memcached.googlecode.com/files/memcached-1.4.9.tar.gz # tar zvxf memc ...
- 平滑处理 - cvSmooth() 函数实现
前言 平滑是图像处理中很重要的操作,其主要用途是降噪.Open CV 提供了 5 种不同的平滑方法:简单模糊,简单无缩放变换的模糊,中值模糊,高斯模糊和双边滤波,它们都通过函数 cvSmooth 实现 ...
- Linux NAT网络连接权威指南
[1]准备工作,写在前面 1.1)检查服务(cmd>>services.msc,我用的是VM) 1.2)确保Vmnet8 连接处于启动状态 + 获取ipv4(ipv6)地址 (在网络连接不 ...
- oracle索引INdex
索引是与表相关的一种可选择数据库对象.索引是为提高数据检索的性能而建立,利用它可快速地确定指定的信息. 索引可建立在一表的一列或多列上,一旦建立,由ORACLE自动维护和使用,对用户是完全透明的.然而 ...
- Android-彻底地理解Binder
转自:https://blog.csdn.net/huachao1001 https://blog.csdn.net/huachao1001/article/details/51504469 你是不是 ...
- 关于 Delphi 中流的使用(7) 压缩与解压缩(TCompressionStream、TDecompressionStream)
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...
- VS中Component Class、User Control及Custom Control的区别 .
.NET Framework 为您提供了开发和实现新控件的能力.除了常见的用户控件外,现在您会发现,您可以编写能执行自身绘图的自定义控件,甚至还可以通过继承扩展现有控件的功能.确定创建何种类型的控件可 ...
- 九度OJ 1061:成绩排序 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:17158 解决:4798 题目描述: 有N个学生的数据,将学生数据按成绩高低排序,如果成绩相同则按姓名字符的字母序排序,如果姓名的字母序也相 ...