二叉树的最大节点
题目
在二叉树中寻找值最大的节点并返回。
样例
给出如下一棵二叉树:
返回值为 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