leetcode

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

maximum-depth.dart (747B)


      1 //This algorithm does a DFS search of the tree to find
      2 //how deep the tree goes. The time complexity of this is
      3 //O(n) where n is the number of tree nodes.
      4 
      5 //Runtime: 271ms Beats: 63.27%
      6 //Memory: 143.6MB Beats: 24.49%
      7 
      8 
      9 
     10 /**
     11  * Definition for a binary tree node.
     12  * class TreeNode {
     13  *   int val;
     14  *   TreeNode? left;
     15  *   TreeNode? right;
     16  *   TreeNode([this.val = 0, this.left, this.right]);
     17  * }
     18  */
     19 class Solution {
     20   int max = 0;
     21   int maxDepth(TreeNode? root) {
     22       recurse(root , 1);
     23       return max;
     24   }
     25   void recurse(TreeNode? node , int depth){
     26       if(node == null){
     27           return;
     28       }
     29       if(depth > max){
     30           max = depth;
     31       }
     32       recurse(node.left, depth + 1);
     33       recurse(node.right , depth + 1);
     34   }
     35 }