commit b43a46f4826ac76b48cf0546c8341ee0621832f7 Author: Mrhongxing <154480715+Mrhongxing@users.noreply.github.com> Date: Mon Oct 12 21:42:06 2026 +0800 0.1 diff --git a/src/com/hong/www/A124.java b/src/com/hong/www/A124.java new file mode 100644 index 0000000..cf645c1 --- /dev/null +++ b/src/com/hong/www/A124.java @@ -0,0 +1,54 @@ +package com.hong.www; + +import java.util.ArrayList; + +public class A124 { + public int aaa(TreeNode root,ArrayList numbers) { + int right=0; + int left=0; + if(root.right != null){ + right=aaa(root.right,numbers); + }; + if(root.left!=null){ + left=aaa(root.left,numbers); + } + if(left>0&&right>0){ + numbers.add(left+right+root.val); + }else if(left>0){ + numbers.add(left+root.val); + }else if(right>0){ + numbers.add(right+root.val); + }else{ + numbers.add(root.val); + } + //numbers.add(left+right+root.val); + if(right>left&&right>0){ + return right+root.val; + }else if(left>right&&left>0){ + return left+root.val; + }else{ + return root.val; + } + //return right+left+root.val; + } + public int maxPathSum(TreeNode root){ + ArrayList numbers=new ArrayList<>(); + int finalcount=Integer.MIN_VALUE; + aaa(root,numbers); + for(int x:numbers){ + if(x>finalcount){ + finalcount=x; + } + } + return finalcount; + } + public static void main(String[] args) { + TreeNode root = new TreeNode(1); + root.left = new TreeNode(1); + root.right = new TreeNode(100); + root.right.left = new TreeNode(2); + root.right.right=new TreeNode(2); + A124 solution = new A124(); + System.out.println(solution.maxPathSum(root)); + } +} \ No newline at end of file diff --git a/src/com/hong/www/TreeNode.java b/src/com/hong/www/TreeNode.java new file mode 100644 index 0000000..128b363 --- /dev/null +++ b/src/com/hong/www/TreeNode.java @@ -0,0 +1,13 @@ +package com.hong.www; + +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + public TreeNode(int x) { val = x; } + public TreeNode(int x, TreeNode left, TreeNode right) { + this.val = x; + this.left = left; + this.right = right; + } +} \ No newline at end of file diff --git a/src/pom.xml b/src/pom.xml new file mode 100644 index 0000000..e69de29