FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
DSA_Problems/LeetCode/0124_BinaryTreeMaximumPathSum.cpp at main · lakshitcodes/DSA_Problems · GitHub
lakshitcodes
/
DSA_Problems
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
DSA_Problems
/
LeetCode
/
0124_BinaryTreeMaximumPathSum.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
40 lines (38 loc) · 1.05 KB
Breadcrumbs
DSA_Problems
/
LeetCode
/
0124_BinaryTreeMaximumPathSum.cpp
Copy path
File metadata and controls
40 lines (38 loc) · 1.05 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
<
bits/stdc++.h
>
using
namespace
std
;
//
Question Link : https://leetcode.com/problems/binary-tree-maximum-path-sum/description/
/*
*
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class
Solution
{
public:
int
solve
(TreeNode *root,
int
&maxi)
{
if
(root ==
nullptr
)
{
return
0
;
}
int
left =
max
(
0
,
solve
(root->
left
, maxi));
int
right =
max
(
0
,
solve
(root->
right
, maxi));
int
ans = root->
val
+
max
(left, right);
maxi =
max
(maxi, root->
val
+ left + right);
return
ans;
}
int
maxPathSum
(TreeNode *root)
{
std::ios_base::sync_with_stdio
(
false
);
std::cin.
tie
(
NULL
);
int
maxi =
INT_MIN
;
solve
(root, maxi);
return
maxi;
}
};
Back
|
FazBrowse Home
|
New Git URL