Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1 \ 2 / 3
return [1,3,2]
.
中序遍历二叉树,先遍历左子树,在变量当前节点,最后是右子树,采用函数递归实现
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void getResult(vector<int> &result,TreeNode* root)
{
if(root->left!=NULL)
getResult(result,root->left);
result.push_back(root->val);
if(root->right!=NULL)
getResult(result,root->right);
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int> result;
if(root == NULL) return result;
getResult(result,root);
return result;
}
};