# 二叉树路径的最大值

Given a binary tree, find the maximum path sum.

For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.

For example:
Given the below binary tree,

       1
/ \
2   3


Return 6.

## 我们先建立求和的思路

/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
class Solution {
private int max;
public int maxPathSum(TreeNode root) {
if (root == null) {
return 0;
}
max = Integer.MIN_VALUE;
countRound(root);
return max;
}
// 这个函数只是计算 在node节点往两边延伸的 一条路径的最大值
public int countRound(TreeNode node) {
if (node == null) {
return 0;
}
// 如果左子树里面的最大路径负数，不计算
int left = Math.max(0,countRound(node.left));
// 如果右子树里面的最大路径负数，不计算
int right = Math.max(0, countRound(node.right));
max = Math.max(max, (left + right) + node.val);
return Math.max(left, right) + node.val;
}
}

Given a binary tree, return the inorder traversal of its nodes’ values.

Example:

Input: [1,null,2,3]
1
\
2
/
3


Output: [1,3,2]

/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
//if (root != null)
List<Integer> res = new ArrayList<>();
// 我们可以使用栈来完成这件事情
Stack<TreeNode> stack = new Stack<>();
TreeNode p = root;
while (p != null || !stack.isEmpty()) {
// 中序遍历优先访问 左孩子节点
while (p != null) {
stack.push(p);
p = p.left;
}
if (!stack.isEmpty()) {
p = stack.pop(); // 出栈元素 为最左节点
p = p.right;
}
}
return res;
}
}

void inOrder(BinTree *root){
if(root!=NULL){
inOrder(root->lchild);
}