Movatterモバイル変換


[0]ホーム

URL:


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

Commit2ca4d34

Browse files
authored
Merge pull requestneetcode-gh#1051 from julienChemillier/patch-9
Add 124 in c language
2 parents3c05c05 +5e5069f commit2ca4d34

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

‎c/124-Binary-Tree-Maximum-Path-Sum.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Given the root of a binary tree, return the maximum path sum of any non-empty path.
3+
Time: O(n)
4+
Space: O(1)
5+
6+
*/
7+
8+
intmax(inta,intb) {
9+
returna>b?a:b;
10+
}
11+
12+
intrec(structTreeNode*t,int*m) {// Return the maximum path sum which uses t->Val
13+
if (t==NULL)
14+
return-1001;
15+
intr=rec(t->right,m);
16+
intl=rec(t->left,m);
17+
if (t->val>0)
18+
*m=max(*m,t->val+max(l+r,max(l,r)));
19+
else
20+
*m=max(*m,max(r,max(l,l+r+t->val)));
21+
returnmax(0,max(l,r))+t->val;
22+
}
23+
24+
intmaxPathSum(structTreeNode*root){
25+
intm=root->val;
26+
intc=rec(root,&m);
27+
returnmax(c,m);
28+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp