lintcode.67 二叉树中序遍历

时间:2023-08-22 23:14:23
lintcode.67 二叉树中序遍历

二叉树的中序遍历 

给出一棵二叉树,返回其中序遍历

您在真实的面试中是否遇到过这个题?
Yes
样例

给出二叉树 {1,#,2,3},

   1
\
2
/
3

返回 [1,3,2].

/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/ class Solution {
public:
/*
* @param root: A Tree
* @return: Inorder in ArrayList which contains node values.
*/
vector<int> l;
vector<int> inorderTraversal(TreeNode * root) {
// write your code here
if(root==NULL)
return l;
inorderTraversal(root->left);
l.push_back(root->val);
inorderTraversal(root->right);
return l;
}
};