Binary Tree Inorder Traversal--leetcode
原题链接:https://oj.leetcode.com/problems/binary-tree-inorder-traversal/
题目大意:中序遍历二叉树
解题思路:中序遍历二叉树。中序遍历二叉树的左子树,訪问根结点,中序遍历二叉树的右子树。非递归实现时,用一个栈模拟遍历过程就可以。由于须要先遍历左子树。所以每一个结点先入栈。出栈时訪问。
vector<int> inorderTraversal(TreeNode *root) {
vector<int> ret;
if(!root)
return ret;
stack<TreeNode*> s;
while(root||!s.empty())
{
if(!root)
{
root=s.top();
ret.push_back(root->val);
s.pop();
root=root->right;
}
else
{
s.push(root);
root=root->left;
}
}
复杂度分析:时间复杂度为O(N),由于每一个结点仅遍历一次。
空间复杂度为O(lgN)。为栈的最大长度。即树深。
中序遍历和先许遍历一样须要熟练掌握,bug-free哦。
Binary Tree Inorder Traversal--leetcode的更多相关文章
- Binary Tree Inorder Traversal -- LeetCode 94
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- Binary Tree Inorder Traversal ——LeetCode
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- Binary Tree Inorder Traversal leetcode java
题目: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binar ...
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal
既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...
- LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...
- 49. leetcode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal 二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack
- LeetCode: Binary Tree Inorder Traversal 解题报告
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
- 【LeetCode】94. Binary Tree Inorder Traversal (3 solutions)
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
- 【LeetCode】Binary Tree Inorder Traversal
Binary Tree Inorder Traversal Total Accepted: 16406 Total Submissions: 47212My Submissions Given a b ...
随机推荐
- Geeks - Range Minimum Query RMQ范围最小值查询
使用线段树预处理.能够使得查询RMQ时间效率在O(lgn). 线段树是记录某范围内的最小值. 标准的线段树应用. Geeks上仅仅有两道线段树的题目了.并且没有讲到pushUp和pushDown操作. ...
- 40.DOM读取XML
main.cpp #include <QtGui> #include <iostream> #include "domparser.h" int main( ...
- svn插件失效
安装其他插件后,可能出现SVN插件失效了,在eclipse中完全找不到SVN的任何操作选项,此时可尝试通过以下办法解决: 把eclipse/configuration目录下的org.eclipse.u ...
- POJ 3660 Floyd传递闭包
题意:牛有强弱,给出一些牛的强弱的胜负关系,问可以确定几头牛的排名. 思路: Floyd传递闭包 // by SiriusRen #include <bitset> #include &l ...
- sqlserver数据文件位置如何迁移
亲测有效的一种方式: 1.对应的数据库脱机 2.迁移物理文件 3.删除原有实例 4.附加
- C# 3.0的新特性
自动属性. 之前定义属性的步骤: private filed + public property. 现在的形式:int id{get;set;}. 可以分别设置get/set的保护级别(protect ...
- canvas实现刮刮卡效果
canvas实现刮刮卡效果 实现步骤: 设置页面背景图,即刮刮卡底部图片 绘制canvas 刮刮卡顶部图片drawImage 绑定事件 addEventListener touchstart.tou ...
- 谈谈javascript中原型继承
什么是继承?拿来主义:自己没有,别人有,把别人的拿过来使用或者让其成为自己的 如何实现继承的方式 原型继承 混入继承 经典继承 1. 混入继承 由于一个对象可以继承自任意的对象,即:o可以继承自对象o ...
- Android WebView访问网站携带登录认证Cookies和动态自定义的cookies
最近项目几个页面要复用微信程序的网页.但是需要调用微网站登录接口,返回Cookies,webview访问需要的网页的时候携带. 并且还需要几个其他的动态改变的cookie,目的是根据这几个动态自定义c ...
- Android中的事件分发机制
Android中的事件分发机制 作者:丁明祥 邮箱:2780087178@qq.com 这篇文章这周之内尽量写完 参考资料: Android事件分发机制完全解析,带你从源码的角度彻底理解(上) And ...