LC 94. Binary Tree Inorder Traversal
问题描述
Given a binary tree, return the inorder traversal of its nodes' values. (左 - 根 - 右)
Example:
Input: [1,null,2,3]
1
\
2
/
3 Output: [1,3,2]
Follow up: Recursive solution is trivial, could you do it iteratively?
参考答案
/**
* Definition for a binary tree node.
* 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) {
// init
vector<int> res;
stack<TreeNode* > st;
TreeNode* p = root; // 初始化根节点 while(p||!st.empty()){
// 一旦遇到节点,先考虑左边的,直到尽头,如果没有之后的右node,就停止运行了
while(p){
st.push(p);
p = p->left;
}
// 立刻提取p的信息,并且把p弹出来。如果进入while,那么这一步只会是左孩子,如果没进入while,那么会是父节点/右节点。
p = st.top();
st.pop();
res.push_back(p->val);
// 把p 变成p的右孩子
p = p->right;
}
return res;
}
};
LC 94. Binary Tree Inorder Traversal的更多相关文章
- 49. leetcode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal 二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- 刷题94. Binary Tree Inorder Traversal
一.题目说明 题目94. Binary Tree Inorder Traversal,给一个二叉树,返回中序遍历序列.题目难度是Medium! 二.我的解答 用递归遍历,学过数据结构的应该都可以实现. ...
- 【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 94. Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- 94. Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- leetcode 94 Binary Tree Inorder Traversal ----- java
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- Java [Leetcode 94]Binary Tree Inorder Traversal
题目描述: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given bina ...
随机推荐
- Mybatis——更新DB表的字段时,应该注意的点
1.记录下哪些表发生了字段更新. 2.利用Navicat将最新的数据库(schema)转储SQL文件到项目的sql目录下,作为备份 3.依次更新 被记录表所对应的Po类,确保类的域和表的字段一一对应, ...
- Maven:element '******' cannot have character [children]
此错误是由于XML文件的解析不正确造成的,因为在一个/某些标签之间存在奇怪和隐藏的字符. 这些字符可能来自网络上的复制粘贴.要解决此问题,请删除标签>标记定义之间的所有空格和换行符,然后将它们放 ...
- 使用PyMySQL连接MySQL错误
使用PyMySQL连接MySQL错误 之前写了一个小项目,今天突然想起来,准备优化一下,但是原本好好的项目竟然跑不起来了 emmm....我真的啥都没干呀 具体错误是这样的: Traceback (m ...
- 记录学习Linux遇到的问题
shl@shl-tx:~$ ifconfig Command 'ifconfig' not found, but can be installed with: sudo apt install net ...
- Django - ORM - 事务, 乐观锁, 悲观锁
事务 概念 Transaction 事务:一个最小的不可再分的工作单元:通常一个事务对应一个完整的业务(例如银行账户转账业务,该业务就是一个最小的工作单元) 一个完整的业务需要批量的DML(inser ...
- 07-08 Flutter仿京东商城项目 商品分类页面布局:Flutter仿京东商城项目 商品分类页面数据渲染
Flutter实战(交流群:452892873) 本项目是一个实战项目,根据目录建文件,并复制从第一节到最新更新的文章,可以构成完整的一个请求后台数据的项目: CateModel.dart class ...
- linux简单命令3---帮助命令
1:帮助命令:man 命令: 2:这个帮助用的比较多(还是中文):命令 --help 3:shell帮助 4:详细命令(比man更详细)帮助,用的少,比较麻烦:info
- MobileNet V2深入理解
转载:https://zhuanlan.zhihu.com/p/33075914 MobileNet V2 论文初读 转载:https://blog.csdn.net/wfei101/article/ ...
- 记录一次vxworks下使用NFS组件的过程
问题:有三块CPU都运行vxworks6.9,现在想要CPU3做server,CPU1-2通过NFS访问CPU3上的文件 补充:使用防火墙可能会影响NFS访问,目前我还没有找到解决办法... 下面是过 ...
- java泛型中特殊符号的含义
java泛型中的标记符含义: E - Element (在集合中使用,因为集合中存放的是元素) T - Type(Java 类) K - Key(键) V - Value(值) N - Number ...