leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0687.cpp (406B)
0 class Solution { 1 int res = 0; 2 3 int rec(const TreeNode *root, int parent) { 4 if (!root) return 0; 5 6 const auto l = rec(root->left, root->val), r = rec(root->right, root->val); 7 8 res = max(res, l + r); 9 return root->val == parent ? max(l, r) + 1 : 0; 10 } 11 12 public: 13 int longestUnivaluePath(const TreeNode *root) { 14 rec(root, -1); 15 return res; 16 } 17 };