// Path: src/commons/datastructures/TreeNode.java
// public class TreeNode {
//     public int val;
//     public TreeNode left;
//     public TreeNode right;
//     public TreeNode(int x) {
//         val = x;
//     }
// 
//     @Override
//     public String toString() {
//         return "TreeNode{" +
//                 "val=" + val +
//                 '}';
//     }
// }


import commons.datastructures.TreeNode;
package BinaryTreeMaximumPathSum;


/**
 * User: Danyang
 * Date: 1/27/2015
 * Time: 21:18
 *
 * Given a binary tree, find the maximum path sum.

 The path may start and end at any node in the tree.

 For example:
 Given the below binary tree,

   1
  / \
 2   3
 Return 6.
 */
public class Solution {
    int gmax = Integer.MIN_VALUE;

    /**
     * Notice:
     * 1. max path end with a node
     * 2. global max may cross a node
     * @param root
     * @return
     */
// Path: src/commons/datastructures/TreeNode.java
// public class TreeNode {
//     public int val;
//     public TreeNode left;
//     public TreeNode right;
//     public TreeNode(int x) {
//         val = x;
//     }
// 
//     @Override
//     public String toString() {
//         return "TreeNode{" +
//                 "val=" + val +
//                 '}';
//     }
// }


// Path: src/BinaryTreeMaximumPathSum/Solution.java
import commons.datastructures.TreeNode;

package BinaryTreeMaximumPathSum;


/**
 * User: Danyang
 * Date: 1/27/2015
 * Time: 21:18
 *
 * Given a binary tree, find the maximum path sum.

 The path may start and end at any node in the tree.

 For example:
 Given the below binary tree,

   1
  / \
 2   3
 Return 6.
 */
public class Solution {
    int gmax = Integer.MIN_VALUE;

    /**
     * Notice:
     * 1. max path end with a node
     * 2. global max may cross a node
     * @param root
     * @return
     */
    public int maxPathSum(TreeNode root) {