二叉树最大节点值

来源:互联网 发布:党章党规网络测试答案 编辑:程序博客网 时间:2024/06/03 22:06
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;    }}
原创粉丝点击