632.Binary Tree Maximum Node-二叉树的最大节点(入门题)

二叉树的最大节点

  1. 题目

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

  2. 样例

    给出如下一棵二叉树:
    这里写图片描述
    返回值为 3 的节点。

  3. 题解

    遍历一遍即可。

public class Solution {/*** @param root the root of binary tree* @return the max ndoe*/private TreeNode max = new TreeNode(Integer.MIN_VALUE);public TreeNode maxNode(TreeNode root) {if (root == null){return root;}max = max.val > root.val ? max : root;maxNode(root.left);maxNode(root.right);return max;}
}

Last Update 2017.4.24