commit 7b6280e38c83e6ee7d4d1e513c4db710e0974360
parent 4c99f0ca5a650469a67124f81a78c0d949711412
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date: Thu, 20 Apr 2023 10:48:53 -0500
Completed maximum depth binary tree in dart
Diffstat:
1 file changed, 35 insertions(+), 0 deletions(-)
diff --git a/maximum-depth/maximum-depth.dart b/maximum-depth/maximum-depth.dart
@@ -0,0 +1,35 @@
+//This algorithm does a DFS search of the tree to find
+//how deep the tree goes. The time complexity of this is
+//O(n) where n is the number of tree nodes.
+
+//Runtime: 271ms Beats: 63.27%
+//Memory: 143.6MB Beats: 24.49%
+
+
+
+/**
+ * Definition for a binary tree node.
+ * class TreeNode {
+ * int val;
+ * TreeNode? left;
+ * TreeNode? right;
+ * TreeNode([this.val = 0, this.left, this.right]);
+ * }
+ */
+class Solution {
+ int max = 0;
+ int maxDepth(TreeNode? root) {
+ recurse(root , 1);
+ return max;
+ }
+ void recurse(TreeNode? node , int depth){
+ if(node == null){
+ return;
+ }
+ if(depth > max){
+ max = depth;
+ }
+ recurse(node.left, depth + 1);
+ recurse(node.right , depth + 1);
+ }
+}