144--二叉树的前序遍历 难度:中等

class Solution {
     
public:
    vector<int> res;
    vector<int> preorderTraversal(TreeNode* root) 
    {
     
        if(root==NULL)
        {
     
            return res;
        }
        preorder(root);
        return res;
    }
    void preorder(TreeNode* root)
    {
     
        if(root==NULL)
        {
     
            return ;
        }
        res.push_back(root->val);
        preorder(root->left);
        preorder(root->right);
    }
};

你可能感兴趣的:(leetcode题解)