【leetcode刷题笔记】Binary Tree 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?
解题:果然不能晚上做题,效率好低。看了讨论才学会的解法。设置一个指针next指向当前访问的节点,如果它不为空,就把它压栈,并且下一个访问它的左节点;如果它为空,就从栈顶一定是它的父节点,取出它的父节点,把这个父节点的值加入向量中,然后去访问父节点的右子。特别注意最后循环结束的条件是栈不空或者next指针不空,因为有可能栈里面的东西弹完了,next还指着一个节点,只有根节点的时候就是这种情况。
代码:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
vector<int> ans;
stack<TreeNode*>s;
if(root == NULL)
return ans; TreeNode* next = root;
while(!s.empty()||next!=NULL){
if(next != NULL)
{
s.push(next);
next = next->left;
}
else{
next = s.top();
s.pop();
ans.push_back(next->val);
next = next->right; } }
return ans;
}
};
【leetcode刷题笔记】Binary Tree Inorder Traversal的更多相关文章
- 刷题94. Binary Tree Inorder Traversal
一.题目说明 题目94. Binary Tree Inorder Traversal,给一个二叉树,返回中序遍历序列.题目难度是Medium! 二.我的解答 用递归遍历,学过数据结构的应该都可以实现. ...
- [刷题] 144 Binary Tree Preorder Traversal
要求 二叉树的前序遍历 实现 递归 栈模拟 定义结构体 Command 模拟指令,字符串s描述命令,树节点node为指令作用的节点 定义栈 Stack 存储命令 1 #include ...
- LeetCode(94)Binary Tree Inorder Traversal
题目如下: Python代码: def inorderTraversal(self, root): res = [] self.helper(root, res) return res def hel ...
- [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal
既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- leetcode -day29 Binary Tree Inorder Traversal & Restore IP Addresses
1. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' ...
- 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 ...
随机推荐
- ylb:使用sql语句实现添加、删除约束
ylbtech-SQL Server:SQL Server-使用sql语句实现添加.删除约束 --主键约束(Primary Key constraint):要求主键列的数据唯一,并且不允许为空. -- ...
- linux常用命令(个人学习笔记)
个人说明:学习linux也有半年左右的时间了,从一开始的只会简单的开关机,到现在的熟悉应用一些简单的命令,还是有些进步的,不过对于我这种菜鸟来说,如果不经常用,发现忘的很快.所以就把在学习过程中遇到的 ...
- scikit-learn---PCA(Principle Component Analysis)---KNN(image classifier)
摘要:PCA为非监督分类方法,常用于数据降维.为监督分类数据预处理,本例采用PCA对人脸特征提取先做降维处理,然后使用KNN算法对图片进行分类 ##1.PCA简介 设法将原来变量重新组合成一组新的互相 ...
- Android开发之Conversion to Dalvik format failed问题解决
[2014-4-21 21:28:06 - Dex Loader] Unable to execute dex: java.nio.BufferOverflowException. Check the ...
- RTM-DSP项目总结
1. 项目介绍 在NINJA设备上支持RTM-ISDN卡 RTM-ISDN卡硬件组成 主要组成单元 C6415: DSP PEB383(上图中的PEX8112改为PEB383,由于后者具有NT功能) ...
- Swagger跨域访问
我们用springboot开发完后,需要前端vue用swagger跨域,默认是不能跨域的,所以需要我们后台设置跨域访问,将下面代码完整复制即可. 在springboot项目中新建class : Cor ...
- OpenCV 3.1
http://www.tuicool.com/articles/FRfMni2 http://docs.opencv.org/3.1.0/d7/d9f/tutorial_linux_install.h ...
- linux网络及防火墙配置命令
/etc/sysconfig/network 包括主机基本网络信息,用于系统启动 /etc/sysconfig/network-script/ 此目录下是系统启动最初始化网络的信息 /etc/sy ...
- [转]Ubuntu Server命令行更换软件源
sucd /etc/aptwget http://mirrors.163.com/.help/sources.list.lucidmv sources.list sources.list.backup ...
- RS-232针脚(8 pin)
1 CD Carrier Detect 载波检测 2 RXD Receive Data 接收 3 TXD Transmit Data 发送 4 DTR Data Terminal Ready ...