Skip to content

Commit

Permalink
Create 124-Binary-Tree-Maximum-Path-Sum.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
agnihotriketan authored Sep 12, 2022
1 parent fd08e46 commit 8fd556a
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions csharp/124-Binary-Tree-Maximum-Path-Sum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
public class Solution {

int maxPathSum = Int32.MinValue;

public int MaxPathSum(TreeNode root)
{
DfsMaxPathSum(root);
return maxPathSum;
}

private int DfsMaxPathSum(TreeNode root)
{
if (root == null)
return 0;

int leftMax = DfsMaxPathSum(root.left),
rightMax = DfsMaxPathSum(root.right),
currentMax = 0;

currentMax = Math.Max(currentMax, Math.Max(leftMax + root.val, rightMax + root.val));
maxPathSum = Math.Max(maxPathSum, leftMax + root.val + rightMax);

return currentMax;
}
}

0 comments on commit 8fd556a

Please sign in to comment.