LintCode | 632. Binary Tree Maximum Node

ind the maximum node in a binary tree, return the node.


水题一道,直接po代码

public class Solution {

    public TreeNode maxNode(TreeNode root) {
        //先判空
        if(root  == null) return null;
        //先将父节点预设为最大值
        TreeNode max = root;
        //分别将左右子节点与最大值点比较得出最大值,递归实现
        if(root.left != null) {
            TreeNode left = maxNode(root.left);
            max = max.val > left.val? max : left;
        }
        if(root.right != null) {
            TreeNode right = maxNode(root.right);
            max = max.val > right.val? max : right;
        }
        return max;
    }
}

你可能感兴趣的:(LintCode)