[Lintcode] 632. Binary Tree Maximum Node

来源:互联网 发布:淘宝虚拟变实物 编辑:程序博客网 时间:2024/06/11 23:00

在二叉树中寻找值最大的节点并返回。

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;    }}
原创粉丝点击