Skip to content

Instantly share code, notes, and snippets.

@esase
Created April 17, 2022 10:18
Show Gist options
  • Save esase/dda30c4f0dbb3d2723983a258fa0d683 to your computer and use it in GitHub Desktop.
Save esase/dda30c4f0dbb3d2723983a258fa0d683 to your computer and use it in GitHub Desktop.
/**
* 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