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 ...
随机推荐
- HDU1237 简单计算器 【栈】+【逆波兰式】
简单计算器 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- zzulioj--1858--单词翻转(模拟)
1858: 单词翻转 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 88 Solved: 35 SubmitStatusWeb Board Desc ...
- angular 兼容ie11 ie11兼容
兼容一(new Date()用法) new Date('2018-01-01 00:00:00').getHours(); new Date('2018-01-01 00:00:00').getMin ...
- windows安装gnvm安装教程,node多版本解决方案
本文是实现windows下node多版本管理 Win10专业版 一.安装前准备 安装前请卸载node相关的所有东西!!! 二.gnvm下载 gnvm搜索 http://ksria.com/gnvm/ ...
- centos + nodejs + egg2.x 开发微信分享功能
本文章发到掘金上,请移步阅读: https://juejin.im/post/5cf10b02e51d45778f076ccd
- 类扩展和category的小区别
类扩展可以给类声明新的变量(属性),但是方法的实现只能在.m中实现 category可以给类声明新的方法实例,但是不可以添加变量(属性)
- jquery 登录,删除提示信息框
<a onclick="return confirm('确认要退出登录吗?')">退出</a> //删除,修改,添加时提示信息框 (del,edit,add ...
- php 生成不重复的随机字符串
md5(uniqid(md5(microtime(true)),true))
- Oracle [sys_connect_by_path] 函数
create table test ( NO NUMBER, VALUE VARCHAR2(100), NAME VARCHAR2(100) ); -------------------------- ...
- JAVA导出csv出现0.00E+00
导出csv出现 0.00E+00的问题,打印其值为0E-8:这是因为数据表中无对应数据(decimal),查询结果则为 0e-8. 出现的字段是多个字段相加产生的和,所以这里调用了一个相加的方法.在相 ...