在二叉树中寻找值最大的节点并返回。
public class Solution {
/**
* @param root the root of binary tree
* @return the max node
*/
public TreeNode maxNode(TreeNode root) {
if (root == null) {
return root;
}
TreeNode left = maxNode(root.left);
TreeNode right = maxNode(root.right);
return max(root, max(left, right));
}
public TreeNode max(TreeNode a, TreeNode b) {
if (a == null) {
return b;
}
if (b == null) {
return a;
}
if (a.val > b.val) {
return a;
}
return b;
}
}