二叉树的中序遍历
来源:互联网 发布:程序员高清图 编辑:程序博客网 时间:2024/06/05 15:37
题目描述:
给出一棵二叉树,返回其中序遍历
给出二叉树 {1,#,2,3}
,
1 \ 2 / 3
返回 [1,3,2]
.
解题思路:
先创建一个指针向量,和前序遍历的思路一样,判断根节点是否为空,若为空,返回该指针向量;若不为空,创建一个函数A,在函数A中,判断根节点是否为空,若根节点为空,返回空;若根节点不为空,按照中序遍历的方法,先访问左子树,再访问根节点,最后访问右子树。
代码实现:
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> p;
if(root==NULL) return p;
A(p,root);
}
void A(vector<int> &p,TreeNode *root)
{
if(root==NULL) return;
A(p,root->left);
p.push_back(root->val);
A(p,root->right);
}
};
感想:
本文按照中序遍历的访问顺序进行访问,思路和前序遍历相同。
- 二叉树的中序遍历
- 二叉树的中序遍历
- 线索二叉树的中序遍历
- 有关二叉树的中序遍历
- 二叉树的中序遍历
- 排序二叉树的中序遍历
- 二叉树的中序遍历算法
- 题目:二叉树的中序遍历
- 二叉树的中序遍历
- 二叉树的中序遍历
- 二叉树的中序遍历
- LintCode -- 二叉树的中序遍历
- lintcode 二叉树的中序遍历
- 二叉树的前后中序遍历
- 二叉树的中序遍历
- 二叉树的中序遍历
- lintcode:二叉树的中序遍历
- lintcode,二叉树的中序遍历
- caffe学习(6)------单张图像测试
- codeforces 707c
- 线性表(上)之顺序存储
- NSObject
- EOF的理解
- 二叉树的中序遍历
- 未来智能手机摄像技术发展趋势:硬件
- 线性表(中)之链式存储
- Reachability
- DialogFragment 创建对话框
- 4.2.1 结构
- Android基础之Json解析(中)
- Linux 链接脚本分析
- 在Mac上安装 jdk node npm ionic Cordova