Recursive Solution C++(0 ms)


  • 0
    R
        vector<int> preorderTraversal(TreeNode* root) {
            path(root);
            return pre_order;
        }
        void path(TreeNode* root) {
         if(root== NULL)
         return ;
         pre_order.push_back(root->val);
         preorderTraversal(root->left);
         preorderTraversal(root->right);
         return ;
        }
    private:
        vector<int>pre_order;
    

    Using Recursive Solution using two function.The algorithm is very simple as you just have push the element in the same order as preoder.


Log in to reply
 

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