-
Notifications
You must be signed in to change notification settings - Fork 1
/
maximum-depth-of-binary-tree.js
51 lines (47 loc) · 1.06 KB
/
maximum-depth-of-binary-tree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* Source: https://leetcode.com/problems/maximum-depth-of-binary-tree/
* Tags: [Tree,Depth-first Search]
* Level: Easy
* Updated: 2015-04-24
* Title: Maximum Depth of Binary Tree
* Auther: @imcoddy
* Content: Given a binary tree, find its maximum depth.
*
* The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
*/
/**
* Definition for binary tree
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @returns {number}
*/
/**
* Memo:
* Complex: O(logn)
* Runtime: 140ms
* Tests: 38 test cases passed
* Rank: A
*/
var maxDepth = function(root) {
return root ? Math.max(maxDepth(root.left), maxDepth(root.right)) + 1 : 0;
};
/**
* Memo:
* Complex: O(logn)
* Runtime: 160ms
* Tests: 38 test cases passed
* Rank: B
*/
var maxDepth = function(root) {
if (!root) {
return 0;
}
var left = maxDepth(root.left);
var right = maxDepth(root.right);
return Math.max(left, right) + 1;
};