http://www.lintcode.com/en/problem/binary-tree-maximum-node/

public class Solution {
    /**
     * @param root the root of binary tree
     * @return the max ndoe
     */
    public TreeNode maxNode(TreeNode root) {
        // Write your code here
        if (root == null) {
            return null;
        }

        TreeNode left = maxNode(root.left);
        TreeNode right = maxNode(root.right);

        TreeNode ans = root;
        if (left != null) {
            ans = root.val > left.val ? root : left;
        }

        if (right != null) {
            ans = ans.val > right.val ? ans : right;
        }

        return ans;

    }
}

results matching ""

    No results matching ""