Skip to content

Commit fab9089

Browse files
Merge pull request algorithm001#561 from hfcc8685/master
[week3][071][hanfeng] week3 homework
2 parents 354d65f + f9d2d89 commit fab9089

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

Week_03/id_71/leetCode_104_71.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {number}
11+
*/
12+
var maxDepth = function(root) {
13+
if (root === null) {
14+
return 0
15+
}
16+
let leftMaxDepth = maxDepth(root.left)
17+
let rightMaxDepth = maxDepth(root.right)
18+
return (leftMaxDepth > rightMaxDepth ? leftMaxDepth : rightMaxDepth) + 1
19+
};

Week_03/id_71/leetCode_997_71.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number} N
3+
* @param {number[][]} trust
4+
* @return {number}
5+
*/
6+
var findJudge = function(N, trust) {
7+
if (N === 1 && trust.length > 0) {
8+
return -1
9+
}
10+
let peopleBeTrusted = new Array(N)
11+
for (let i = 0; i < peopleBeTrusted.length; i++) {
12+
peopleBeTrusted[i] = 0
13+
}
14+
for (let i = 0; i < trust.length; i++) {
15+
peopleBeTrusted[trust[i][0] - 1] = -1
16+
if (peopleBeTrusted[trust[i][1] - 1] !== -1) {
17+
peopleBeTrusted[trust[i][1] - 1]++
18+
}
19+
}
20+
let judge = []
21+
for (let i = 0; i < peopleBeTrusted.length; i++) {
22+
if (peopleBeTrusted[i] === N - 1) {
23+
judge.push(i)
24+
}
25+
}
26+
if (judge.length === 1) {
27+
return judge[0] + 1
28+
}
29+
return -1
30+
};

0 commit comments

Comments
 (0)