Created
April 17, 2022 10:18
-
-
Save esase/dda30c4f0dbb3d2723983a258fa0d683 to your computer and use it in GitHub Desktop.
Maximum depth of binary tree [https://leetcode.com/problems/maximum-depth-of-binary-tree]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 = function(root) { | |
if (!root) { | |
return 0; | |
} | |
return Math.max( | |
getDepthValue(root.left, 1), | |
getDepthValue(root.right, 1) | |
); | |
}; | |
var getDepthValue = function(node, level) { | |
if (!node) { | |
return level; | |
} | |
level++; | |
return Math.max( | |
getDepthValue(node.left, level), | |
getDepthValue(node.right, level) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment