【一天一道LeetCode】#94. Binary Tree Inorder Traversal
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
Given a binary tree, return the inorder traversal of its nodes’ values.
For example:
Given binary tree [1,null,2,3],1
\
2
/
3
return [1,3,2].
(二)解题
题目大意:给定一个二叉树,输出中序遍历
讲二叉树的时候基本上都讲过递归求解中序遍历。即先访问左子树, 再根结点,再右子树,
/**
* 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> ret;
inorder(root,ret);
return ret;
}
void inorder(TreeNode* p,vector<int>& ret)
{
if(p==NULL) return;
inorder(p->left,ret);//访问左子树
ret.push_back(p->val);//将根节点保存
inorder(p->right,ret);//访问右子树
}
};
【一天一道LeetCode】#94. Binary Tree Inorder Traversal的更多相关文章
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- 49. leetcode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal 二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack
- Leetcode 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 ...
- Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
- LeetCode 94. Binary Tree Inorder Traversal 二叉树的中序遍历 C++
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...
- [leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
- leetCode 94.Binary Tree Inorder Traversal(二叉树中序遍历) 解题思路和方法
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
- [LeetCode] 94. Binary Tree Inorder Traversal(二叉树的中序遍历) ☆☆☆
二叉树遍历(前序.中序.后序.层次.深度优先.广度优先遍历) 描述 解析 递归方案 很简单,先左孩子,输出根,再右孩子. 非递归方案 因为访问左孩子后要访问右孩子,所以需要栈这样的数据结构. 1.指针 ...
随机推荐
- js遍历 for-of
for-of遍历 entries() 返回一个遍历器对象,用来遍历[键名, 键值]组成的数组.对于数组,键名就是索引值:对于 Set,键名与键值相同.Map 结构的 Iterator 接口,默认就是调 ...
- 小白的Python之路_day2
Python 的逻辑运算符具有短路原则,例如: or 运算符前面只要是 True,后面都不需要看了,结果就是 True. Python 中表示为真必须用 True,如果用 true 则会当成是变量, ...
- Oracle中时间和日期函数总结
查看当前日期格式:select * from nls_session_parameters where parameter='NLS_DATE_FORMAT'; 修改日期的格式: alter sess ...
- Python中的转义
在Python交互式解释器中,输出的字符串会用引号引起来,特殊字符会用反斜杠(\)转义.如果遇到带有\的字符被当作特殊字符时,有以下两种处理方法:1.使用双反斜杠(\\)来转义2.使用原始字符串,方法 ...
- NDK编程的一个坑—Arm平台下的类型转换
最近在做DNN定点化相关的工作,DNN定点化就是把float表示的模型压缩成char表示,虽然会损失精度,但是由于DNN训练的模型值比较接近且范围较小,实际上带来的性能损失非常小.DNN定点化的好处是 ...
- linux2.6.37内核接两个硬盘导致读写效率变低的问题
一.问题分析: 通过跟踪定位write系统调用的实现发现,在每次调用a_ops->write_end之后,都会去调用balance_dirty_pages_ratelimited,该函数负责检查 ...
- XML Condition And
<Target Name="CustomBuildStep" Condition="'@(CustomBuildStep)' != '' and '$(Select ...
- Java异常处理-----java异常体系
再三思考后还是决定贴图,csdn的格式,我是真玩不转,对不起了各位,继续将就吧. 错误原因:内存溢出.需要的内存已经超出了java虚拟机管理的内存范围. 错误原因:找不到类文件. 错误(Error): ...
- [error]error while loading shared libraries: libpcre.so.1 解决
nginx 安装好之后,启动的时候报错 [root@localhost nginx-1.6.2]# /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin ...
- ZAB协议
zookeeper依赖zab协议来实现分布式数据一致性.基于该协议,zookeeper实现了一种主备模式的系统架构来保持ZooKeeper为高可用的一致性协调框架,自然的ZooKeeper也有着一致性 ...