leetcode

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

commit 8b5d3f89b4869d3cb0ed361cfc8f45f68fb05511
parent 155b1a0c8ce756e41c08acb51e103105ba52c78e
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date:   Sat,  6 May 2023 11:22:29 -0500

Completed binary tree paths problem using dart

Diffstat:
Abinary-tree-paths/binary-tree-paths.dart | 33+++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+), 0 deletions(-)

diff --git a/binary-tree-paths/binary-tree-paths.dart b/binary-tree-paths/binary-tree-paths.dart @@ -0,0 +1,33 @@ +//Find all paths to every leaf in a binary tree and return them as a string. +//The time complexity of this code is O(n) where n is the number of nodes in the tree. +//Time: 261ms Beats: 100% +//Memory: 142.6MB Beats: 100% + +class Solution { + List<String> binaryTreePaths(TreeNode? root) { + recurse(root, ""); + return leaves; + } + List<String> leaves = []; + void recurse(TreeNode? root , String path){ + String add = "->"; + if(path == ""){ + add = ""; + } + if(root?.left == null && root?.right == null){ + path += add + (root?.val ?? 0).toString(); + leaves.add(path); + return; + } + else{ + + if(root?.right != null){ + recurse(root?.right , path + add + (root?.val ?? 0).toString()); + } + if(root?.left != null){ + recurse(root?.left, path + add + (root?.val ?? 0).toString()); + + } + } + } +}