Skip to content

Commit d0dd09f

Browse files
committed
Maximum Depth of Binary Tree
1 parent 9e39a15 commit d0dd09f

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Maximum Depth of Binary Tree.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*Given a binary tree, find its maximum depth.
2+
3+
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
4+
5+
For example:
6+
Given binary tree [3,9,20,null,null,15,7],
7+
8+
3
9+
/ \
10+
9 20
11+
/ \
12+
15 7
13+
return its depth = 3.*/
14+
15+
/**
16+
* Definition for a binary tree node.
17+
* function TreeNode(val) {
18+
* this.val = val;
19+
* this.left = this.right = null;
20+
* }
21+
*/
22+
/**
23+
* @param {TreeNode} root
24+
* @return {number}
25+
*/
26+
var maxDepth = function(root) {
27+
if(root == null)
28+
return 0
29+
return 1 + max(maxDepth(root.left),maxDepth(root.right))
30+
};
31+
32+
var max = function(a,b){
33+
return a>b?a:b
34+
};

0 commit comments

Comments
 (0)