Skip to content

Commit 13f6e35

Browse files
Create maximum_depth_binary_tree.js
1 parent b7a0bf0 commit 13f6e35

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

maximum_depth_binary_tree.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number}
12+
*/
13+
var maxDepth = function(root) {
14+
return getDepth(root);
15+
};
16+
17+
var getDepth = function(root) {
18+
if(root === null)
19+
return 0;
20+
21+
let left = getDepth(root.left);
22+
let right = getDepth(root.right);
23+
24+
if(left > right)
25+
return left + 1;
26+
else
27+
return right + 1;
28+
}

0 commit comments

Comments
 (0)