Binary Tree Inorder Traversal

二叉树的中序遍历,没啥好说的

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int>ans;
    void dfs(TreeNode *root){
        if(!root) return ;
        dfs(root->left);
        ans.push_back(root->val);
        dfs(root->right);
    }
    vector<int> inorderTraversal(TreeNode *root) {
        dfs(root);
        return ans;
    }
};

 

posted @ 2015-03-14 15:16  SprayT  阅读(94)  评论(0编辑  收藏  举报