LeetCode - Binary Tree Preorder Traversal

class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        if(!root) return {};
        vector<int> res;
        res.push_back(root->val);
        vector<int> left = preorderTraversal(root->left);
        if(!left.empty()) res.insert(res.end(), left.begin(), left.end());
        vector<int> right = preorderTraversal(root->right);
        if(!right.empty()) res.insert(res.end(), right.begin(), right.end());
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/real_lisa/article/details/83181112