在二叉树中寻找值最大的节点并返回。
样例1:
输入:
{1,-5,3,1,2,-4,-5}
输出: 3
说明:
这棵树如下所示:
1
/ \
-5 3
/ \ / \
1 2 -4 -5
public TreeNode maxNode(TreeNode root) {
ArrayList<TreeNode> result=new ArrayList<>();
result.add(root);
search(root,result);
return result.get(0);
}
public static void search(TreeNode root,ArrayList<TreeNode> result){
if (root==null){
return;
}
if (result.get(0).val<root.val){
result.set(0,root);
}
if (root.left!=null){
search(root.left,result);
}
if (root.right!=null){
search(root.right,result);
}
}