leetcode

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

commit e58286d7a6e800ab3a8e535874a79b32243bc9f6
parent 7e8db1c42c55ac40ae3c7e1bbbdd3e417717e8d7
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date:   Fri,  5 May 2023 07:51:41 -0500

Completed find mode of binary tree using dart

Diffstat:
Afind-mode-in-binary-search-tree/mode-of-binary-tree.dart | 47+++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 47 insertions(+), 0 deletions(-)

diff --git a/find-mode-in-binary-search-tree/mode-of-binary-tree.dart b/find-mode-in-binary-search-tree/mode-of-binary-tree.dart @@ -0,0 +1,47 @@ +//Given a binary search tree find the mode value(s) and +//return it in a list. To solve this problem I iterated through +//the tree and added each value to a map where the value was the number +//of instances of a given number. Then I iterated through the map +//to find the value with the highest number of occurences and returned +//the associated key. +//The time complexity of this code is O(n) where n is the number of nodes. +//Time: 307ms Beats: 100% +//Memory: 145.7MB Beats: 100% +//Not enough submissions for comparsion metrics. + +/** + * Definition for a binary tree node. + * class TreeNode { + * int val; + * TreeNode? left; + * TreeNode? right; + * TreeNode([this.val = 0, this.left, this.right]); + * } + */ +class Solution { + List<int> findMode(TreeNode? root) { + int usages = 0; + List<int> return_vals = [0]; + recurse(root); + for(var entries in vals.entries){ + if(entries.value > usages){ + usages = entries.value; + return_vals = [entries.key]; + } + else if(entries.value == usages){ + return_vals.add(entries.key); + } + } + return return_vals; + } + Map<int, int> vals = {}; + + void recurse(TreeNode? root){ + if(root == null){ + return; + } + vals[root.val] = (vals[root.val] ?? 0) + 1; + recurse(root.left); + recurse(root.right); + } +}