您的位置:首页 > 其它

LeetCode(144) Binary Tree Preorder Traversal

2015-07-26 19:40 363 查看
[code]/**
 * 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 dfs(TreeNode *root, vector<int> &result) {
        if(NULL == root)

            return;

        result.push_back(root->val);
        dfs(root->left, result);
        dfs(root->right, result);

    }
    vector<int> preorderTraversal(TreeNode* root) {

        vector<int> result;
        dfs(root, result);

        return result;

    }
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: