给出一棵二叉树,返回其中序遍历
样例
给出二叉树 {1,#,2,3}
,
1 \ 2 / 3
返回 [1,3,2]
解题思路:dfs遍历中序储存。
class Solution {
/**
* @param root: The root of binary tree.
* @return: Inorder in vector which contains node values.
*/
vector<int>ans;
public:
vector<int> inorderTraversal(TreeNode *root) {
// write your code here
if(root==NULL)return ans;
inorderTraversal(root->left);
ans.push_back(root->val);
inorderTraversal(root->right);
return ans;
}
};