public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
private static long max = 0;
private static long FindTotal(TreeNode root)
return FindTotal(root.left) + FindTotal(root.right) + root.val;
private static long FindMax(TreeNode root, long sum)
long localSum = FindMax(root.left, sum) + FindMax(root.right, sum) + root.val;
max = Math.Max(max, localSum * (sum - localSum));
public static int FindMaxProduct(TreeNode root)
long totalSum = FindTotal(root);
public static void Main()
Console.WriteLine("UniLecs");
var root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
root.right.left = new TreeNode(6);
Console.WriteLine(FindMaxProduct(root));