leetcode

Leetcode submissions
git clone git://git.laack.co/leetcode.git
Log | Files | Refs | README

preorder-traversal.dart (626B)


      1 //Return the preordered list of a binary tree.
      2 //A preorder of a tree is the root then the next 
      3 //node on the left and so on until the bottom until you
      4 //return the right and so on.
      5 //The time complexity of this code is O(n) where n is the number
      6 //of nodes.
      7 //Time: 264ms Beats: 50%
      8 //Memory: 142.4MB Beats: 10%
      9 class Solution {
     10   List<int> vals = [];
     11   List<int> preorderTraversal(TreeNode? root) {
     12       recurse(root);
     13       return vals;
     14   }
     15 
     16   void recurse(TreeNode? root){
     17       if(root == null){
     18           return;
     19       }
     20       vals.add((root?.val ?? 0));
     21       recurse(root?.left);
     22       recurse(root?.right);
     23 
     24 
     25   }
     26 }