LintCode-二叉树的中序遍历
来源:互联网 发布:北京青年旅舍.知乎 编辑:程序博客网 时间:2024/05/21 08:41
题目描述:给出一棵二叉树,返回其中序遍历
样例:
给出二叉树 {1,#,2,3}
,
1 \ 2 / 3
返回 [1,3,2]
.
做题思路:递归算法,先遍历左子树,在看根节点,再遍历右子树,遍历后的结点放进数组中,返回数组。
关键代码:class Solution {
/**
* @param root: The root of binary tree.
* @return: Inorder in vector which contains node values.
*/
public:
std::vector<int> w;
vector<int> inorderTraversal(TreeNode *root) {
// write your code here
if(root==NULL) return w;
else { inorderTraversal(root->left);
w.push_back(root->val);
inorderTraversal(root->right);
}
return w;
}
};
做题感想:和先根遍历一样,只不过把存入数组的语句位置变了。
0 0
- LintCode -- 二叉树的中序遍历
- lintcode 二叉树的中序遍历
- lintcode:二叉树的中序遍历
- lintcode,二叉树的中序遍历
- LintCode-二叉树的中序遍历
- Lintcode 二叉树的中序遍历
- Lintcode 二叉树的中序遍历
- LintCode 二叉树的中序遍历
- 二叉树的中序遍历-LintCode
- lintcode二叉树的中序遍历
- LintCode 二叉树的中序遍历
- lintcode--二叉树的中序遍历
- lintcode--二叉树的中序遍历
- lintcode-二叉树的中序遍历(非递归)-67
- Lintcode 67 二叉树的中序遍历
- lintcode——二叉树的中序遍历
- lintcode 67 二叉树的中序遍历
- LintCode 68 二叉树的中序遍历
- ==和===区别
- 将java源码打成jar包
- 京东 淘宝 的图片放大镜功能
- 软件版本更新
- poj2559 Largest Rectangle in a Histogram
- LintCode-二叉树的中序遍历
- 设置UITextView光标从起始位置开始
- JavaScript (02)-大话JS,屌丝逆袭(一)
- Scala中的下划线到底有多少种应用场景?
- c++的STL的deque
- Build.VERSION_CODES.HONEYCOMB
- 二叉树的最大深度
- Scala中的下划线到底有多少种应用场景?
- SimpleDateFormat线程安全问题解决方案