Пример #1
0
        private static int CalculateRightSolution(TreeNodeSolution solution)
        {
            if (solution == null)
            {
                return(0);
            }

            var result = Math.Max(solution.val, solution.val + Math.Max(CalculateLeftSolution(solution.right as TreeNodeSolution), CalculateRightSolution(solution.right as TreeNodeSolution)));

            solution.SumLeftRight += result - solution.val;
            return(result);
        }
Пример #2
0
        public static TreeNodeSolution GetFromTreeNode(TreeNode similar, List <TreeNodeSolution> solutions)
        {
            TreeNodeSolution result = new TreeNodeSolution(similar.val);

            if (similar.left != null)
            {
                var left = GetFromTreeNode(similar.left, solutions);
                result.left = left;
            }
            if (similar.right != null)
            {
                var right = GetFromTreeNode(similar.right, solutions);
                result.right = right;
            }
            solutions.Add(result);

            return(result);
        }
Пример #3
0
 public int MaxPathSum(TreeNode root)
 {
     return(TreeNodeSolution.CalculateSolution(root));
 }