Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Binary Tree Max path Sum
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; /* Input: [-10,9,20,null,null,15,7] -10 / \ 9 20 / \ 15 7 Output: 42 (max path is 15->20->7) */ class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } class Rextester { static int maxVal = -Integer.MAX_VALUE; public static void main(String args[]) { System.out.println("Hello, World!"); } public static int maxPathSum(TreeNode root) { helper(root); return maxVal; } public static int helper(TreeNode node){ if(node==null) return 0; int leftMax = Math.max(helper(node.left),0); int rightMax = Math.max(helper(node.right),0); maxVal = Math.max(maxVal, node.val+leftMax+rightMax); return node.val+Math.max(leftMax,rightMax); } }
run
|
edit
|
history
|
help
0
Simple imitation for show banners on priorities
exp5
Sourab Ghosh @ IP Address
pow binary
Num ways to decode a string -Facebook interview
1.7
Inheritance
MyBankAppFinal
Alllocate and release memory
Question FizzBuzz