[Tree]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?
/**
* 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) {
vector<int> res;
stack<TreeNode*> stk;
while(root || !stk.empty()){
while(root){
stk.push(root);
root=root->left;
}
if(!stk.empty()){
root = stk.top();
stk.pop();
res.push_back(root->val);
root = root->right;
}
}
return res;
}
};
[Tree]Binary Tree Inorder Traversal的更多相关文章
- leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree
leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...
- LeetCode & tree & binary tree
LeetCode & tree & binary tree 树 & 二叉树 refs https://leetcode.com/problemset/all/?topicSlu ...
- [Tree]Binary Tree Preorder Traversal
Total Accepted: 97599 Total Submissions: 257736 Difficulty: Medium Given a binary tree, return the p ...
- CCI4.4/LintCode Balanced Binary Tree, Binary Tree, Binary Search Tree
Binary Tree: 0到2个子节点; Binary Search Tree: 所有左边的子节点 < node自身 < 所有右边的子节点: 1. Full类型: 除最下面一层外, 每一 ...
- LeetCode_Lowest Common Ancestor of a Binary Search Tree (Binary Tree)
Lowest Common Ancestor of a Binary Search Tree 一.题目描写叙述 二.思路及代码 二叉搜索树有个性质:左子树的值都比根节点小,右子树的值比根节点大.那么我 ...
- [LeetCode] 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
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- Leetcode Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
随机推荐
- Jquery如何获取控件ID
l 1.#id 用法: $(”#myDiv”); 返回值 单个元素的组成的集合 说明: 这个就是直接选择html中的id=”myDiv” l 2.Element 用法: ...
- 跨服务器的sql使用
由于想从别的服务器上的数据库导入一些数据过来 经网上查阅,得到 select * from openrowset( 'SQLOLEDB', '服务器名字'; '用户名'; '密码',数据库名字.dbo ...
- EasyUI时间格式化
changeDateFormatNodate: function (cellval) { var date = new Date(parseInt(cellval.replace("/Dat ...
- 全分布式环境下,DataNode不启动的问题解决
问题出现:机器重启之后,再次在master结点上面执行start-all.sh,发现有一个datanode没有启动,通过jps检查之后,发现slave1上面的datanode进程未启动 原因:每次na ...
- WIFI网络访问(一)
一,WIFI 网卡有哪些状态? WIFI 总共有以下五个状态,实际就是一些整形常量: 1. WIFI_STATE_DISABLED : WIFI 不能使用,其值是: 1 . 2. WIFI_S ...
- Category类别
1.在已有类的基础上进行扩展,无需像继承一样子类化,就可以直接添加一些方法 2.继承不仅可以添加方法还可以添加属性,类别只能添加方法 3.类别不会改变现有类的方法,万一重写,自己写的优先级高 4.把类 ...
- ThinkPHP框架搭建及常见问题(Apache或MySQL无法启动)----简单的初体验
有一定基础的人勿进,这篇讲的只是零基础入门,都是我刚接触以及我所了解到的人刚开始有疑惑的地方,具体框架介绍会在后面的博客中介绍 这一篇只是为了一个简单的页面显示而介绍的方法,不涉及代码,开发环境,所以 ...
- php zendstudio 常用的一些自定义模板标签
<?php /** * xxx.php * ============================================== * Copy right 2013-2016 http: ...
- 一种获取spring环境上下文方法:SpringContextUtil
获得spring里注册Bean的有好几种方法,这里介绍一种比较简单的方法: import org.springframework.beans.BeansException; import org.sp ...
- STM32F051关于printf函数在串口打印中的使用
1.需要在Options for Target -> Code Generation 中勾选Use MicroLIB: 2.需要加入下面这个函数: int fputc(int ch, FILE ...