[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 ...
随机推荐
- dl dt dd标签
<dl>标记定义了一个定义列表,定义列表中的条目是通过使用<dt>标记(“definition title”,定义标题)和<dd>标记(“definition de ...
- JQ 操作样式,背景切换
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- ‘Microsoft.Jet.OLEDB.4.0’ provider is not registered
正如以下msdn论坛所述,Microsoft.Jet.OLEDB.4.0只有32bit,所以我们必须compile application to x86 platform. http://social ...
- URL & webkitURL
base64带来的坑 在web中想要是实现图片在线预览的方式有几种 1.先上传 2.使用FileReader对象 3.URL.createObjectURL(file|blob) base64 能将图 ...
- Java并发编程之闭锁简介
闭锁相当于一扇门,在闭锁到达结束状态之前,这扇门一直是关闭着的,没有任何线程可以通过,当到达结束状态时,这扇门才会打开并容许所有线程通过.它可以使一个或多个线程等待一组事件发生.闭锁状态包括一个计数器 ...
- Java数组的复制
初学Java的时候,需要复制数组的时候,一下子就想到使用赋值语句“=”,例如:array1 = array2:但后来慢慢发现,这个语句并不能将array2的内容复制给array1,而是将array2的 ...
- 用jquery制作加载条
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 总结:整理 oracle异常错误处理 .
5.1 异常处理概念 5.1.1 预定义的异常处理 5.1.2 非预定义的异常处理 5.1.3 用户自定义的异常处理 5.1.4 用户定义的异常处理 5.2 异常错误传播 5.2.1 在执行部分引发 ...
- PHP环境出现时区问题
如下图所示,php执行的时候出现如下提示: 解决办法: 重新设置php编译的时区. 具体方法如下: 1. 找到php.ini文件: 2. 修改php.ini文件中的date.timezone的值,将其 ...
- LINUX下QT与C语言通过网卡名获取网卡IP与MAC
1.QT下 QString RuntimeConfig::ipAddress(QString network) { QList<QNetworkAddressEntry> list; QS ...