Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2049b89

Browse files
update: 287
1 parent ec9dc03 commit 2049b89

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,4 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
143143
|516|[Longest Palindromic Subsequence](https://leetcode.com/problems/longest-palindromic-subsequence/) | [JavaScript](./src/longest-palindromic-subsequence/res.js)|Medium|
144144
|523|[Continuous Subarray Sum](https://leetcode.com/problems/continuous-subarray-sum/) | [JavaScript](./src/continuous-subarray-sum/res.js)|Medium|
145145
|539|[Minimum Time Difference](https://leetcode.com/problems/minimum-time-difference/) | [JavaScript](./src/minimum-time-difference/res.js)|Medium|
146+
|687|[Longest Univalue Path](https://leetcode.com/problems/longest-univalue-path/) | [JavaScript](./src/longest-univalue-path/res.js)|Easy|

‎src/longest-univalue-path/res.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 longestUnivaluePath = function(root) {
13+
if (!root) return 0;
14+
let compRes = 0;
15+
16+
const getMaxPath = (ele) => {
17+
if (ele == null) return 0;
18+
19+
const val = ele.val;
20+
let left = 0;
21+
let right = 0;
22+
let tl = getMaxPath(ele.left);
23+
let tr = getMaxPath(ele.right);
24+
25+
if (ele.left !== null && ele.left.val === val) {
26+
left = tl + 1;
27+
}
28+
if (ele.right !== null && ele.right.val === val) {
29+
right = tr + 1;
30+
}
31+
32+
compRes = Math.max(compRes, left + right);
33+
return Math.max(left, right);
34+
}
35+
36+
getMaxPath(root);
37+
return compRes;
38+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /