leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit d5d4fad717c416c1586c24d3b277d72518ca8924
parent 7760d9501adef9876c9ddaf7ca025ab7f47f4c55
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date:   Wed, 10 May 2023 21:00:24 -0500

Completed binary tree preorder traversal problem using dart

Diffstat:
Abinary-tree-preeorder-traversal/preorder-traversal.dart | 26++++++++++++++++++++++++++
1 file changed, 26 insertions(+), 0 deletions(-)

diff --git a/binary-tree-preeorder-traversal/preorder-traversal.dart b/binary-tree-preeorder-traversal/preorder-traversal.dart @@ -0,0 +1,26 @@ +//Return the preordered list of a binary tree. +//A preorder of a tree is the root then the next +//node on the left and so on until the bottom until you +//return the right and so on. +//The time complexity of this code is O(n) where n is the number +//of nodes. +//Time: 264ms Beats: 50% +//Memory: 142.4MB Beats: 10% +class Solution { + List<int> vals = []; + List<int> preorderTraversal(TreeNode? root) { + recurse(root); + return vals; + } + + void recurse(TreeNode? root){ + if(root == null){ + return; + } + vals.add((root?.val ?? 0)); + recurse(root?.left); + recurse(root?.right); + + + } +}