Skip to content

Commit

Permalink
Create 104-Maximum-Depth-of-Binary-Tree.js
Browse files Browse the repository at this point in the history
  • Loading branch information
veerbia authored Apr 3, 2022
1 parent 101652d commit 096fad5
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions javascript/104-Maximum-Depth-of-Binary-Tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var maxDepth = (root) => {
let maxDepth = 0;
let DFS = (node, depth) => {
if (!node) return maxDepth;
if (depth > maxDepth) maxDepth = depth;
DFS(node.right, depth + 1);
DFS(node.left, depth + 1);
}
DFS(root, 1);
return maxDepth;
};

0 comments on commit 096fad5

Please sign in to comment.