Neat C++ solution


  • 0
    S
    /**
     * 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 preOrder(vector<int> &res, TreeNode *head) {
            if(head && head->val != '#') {
                res.push_back(head->val);
                preOrder(res, head->left);
                preOrder(res, head->right);
            }
        }
    
        vector<int> preorderTraversal(TreeNode* root) {
            vector<int> res;
            preOrder(res, root);
            return res;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.