leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 92feeaa0bbc3c064f641eeb5c4405c23196b7108
parent 3ba74068c5d5089d6fe5b0e19a650fdfa09c74da
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Fri, 22 Nov 2024 16:03:41 +0100

1 Random Problem

Diffstat:
AProblems/0687.cpp | 18++++++++++++++++++
MREADME.md | 1+
2 files changed, 19 insertions(+), 0 deletions(-)

diff --git a/Problems/0687.cpp b/Problems/0687.cpp @@ -0,0 +1,18 @@ +class Solution { + int res = 0; + + int rec(const TreeNode *root, int parent) { + if (!root) return 0; + + const auto l = rec(root->left, root->val), r = rec(root->right, root->val); + + res = max(res, l + r); + return root->val == parent ? max(l, r) + 1 : 0; + } + + public: + int longestUnivaluePath(const TreeNode *root) { + rec(root, -1); + return res; + } +}; diff --git a/README.md b/README.md @@ -529,6 +529,7 @@ reference and a base for solving problems. | 0677 | Medium | [Map Sum Pairs](Problems/0677.cpp) | | 0678 | Medium | [Valid Parenthesis String](Problems/0678.cpp) | | 0684 | Medium | [Redundant Connection](Problems/0684.cpp) | +| 0687 | Medium | [Longest Univalue Path](Problems/0687.cpp) | | 0688 | Medium | [Knight Probability in Chessboard](Problems/0688.cpp) | | 0690 | Medium | [Employee Importance](Problems/0690.cpp) | | 0692 | Medium | [Top K Frequent Words](Problems/0692.cpp) |