public class Solution {
/**
* @param root the root of binary tree
* @return the max ndoe
*/
public TreeNode maxNode(TreeNode root) {
// Write your code here
TreeNode max= new TreeNode(-1000);
if(root!=null){
max=max(max,root);
if(root.left!=null){
max=max(max,root.left);
maxNode(root.left);
}
if(root.right!=null){
max=max(max,root.right);
maxNode(root.right);
}
}
else
max=null;
return max;
}
public TreeNode max(TreeNode n1,TreeNode n2){
if(n1.val>n2.val)
return n1;
else
return n2;
}
}
二叉树最大节点值
最新推荐文章于 2022-03-25 18:38:30 发布