二叉树的中序遍历
来源:互联网 发布:网络招聘的优劣势 编辑:程序博客网 时间:2024/05/21 09:49
1.问题描述
给出一棵二叉树,返回其中序遍历。
样例
给出二叉树 {1,#,2,3}
,
1 \ 2 / 3
返回 [1,3,2]
.
2.解题思路
运用递归的方式,按先左子树然后根节点最后右子树的思想将节点一个个保存到vector中。
3.代码实现
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
/**
* @param root: The root of binary tree.
* @return: Inorder in vector which contains node values.
*/
public:
vector<int> inorderTraversal(TreeNode *root) {
// write your code here
vector<int>r;
inorder(r,root);
return r;
}
void inorder(vector<int>& r,TreeNode*root)
{
if(root==NULL)
return;
inorder(r,root->left);
r.push_back(root->val);
inorder(r,root->right);
}
};
4.感想
和前序遍历一样,vector 只能定义一次。
- 二叉树的中序遍历
- 二叉树的中序遍历
- 线索二叉树的中序遍历
- 有关二叉树的中序遍历
- 二叉树的中序遍历
- 排序二叉树的中序遍历
- 二叉树的中序遍历算法
- 题目:二叉树的中序遍历
- 二叉树的中序遍历
- 二叉树的中序遍历
- 二叉树的中序遍历
- LintCode -- 二叉树的中序遍历
- lintcode 二叉树的中序遍历
- 二叉树的前后中序遍历
- 二叉树的中序遍历
- 二叉树的中序遍历
- lintcode:二叉树的中序遍历
- lintcode,二叉树的中序遍历
- CI中引用外部类库报出“ Cannot redeclare class IOFactory”错误
- date类实现操作符重载
- [leetCode刷题笔记]2017.04.17
- 出现 org.springframework.beans.factory.BeanCreationException 异常的原因及解决方法
- Android中如何监听手机电话状态
- 二叉树的中序遍历
- ext 简单使用
- Linux 虚拟文件系统(二)Mount、Open;设备文件、挂载和打开文件
- JAVA的反射总结
- SketchUp建模然后导入unity3d中的问题
- TCP的成块数据流
- nyoj-独木舟上的旅行
- 求s=a+aa+aaa+……+aa...a
- iOS 解析json或xml swift3网络访问(一)