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

Example

Given a binary tree:

     1
/ \
-5 2
/ \ / \
0 3 -4 -5

return the node with value 3.

 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 root;
TreeNode leftMax = null;
if(root.left!=null){
leftMax = maxNode(root.left);
}
TreeNode rightMax = null;
if(root.right!=null){
rightMax = maxNode(root.right);
}
TreeNode max = root;
if(leftMax!=null){
max = leftMax.val>max.val? leftMax : max;
}
if(rightMax!=null){
max = rightMax.val>max.val? rightMax : max;
}
return max;
}
}

Binary Tree Maximum Node的更多相关文章

  1. 632. Binary Tree Maximum Node【Naive】

    Find the maximum node in a binary tree, return the node. Example Given a binary tree: 1 / \ -5 2 / \ ...

  2. [leetcode]Binary Tree Maximum Path Sum

    Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...

  3. 【leetcode】Binary Tree Maximum Path Sum

    Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...

  4. 26. Binary Tree Maximum Path Sum

    Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...

  5. leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)

    124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大pa ...

  6. LeetCode: Binary Tree Maximum Path Sum 解题报告

    Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and e ...

  7. 【LeetCode】124. Binary Tree Maximum Path Sum

    Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...

  8. 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum

    题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...

  9. 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)

    124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...

随机推荐

  1. db2空值、null

    1. 输入参数为字符类型,且允许为空的,可以使用COALESCE(inputParameter,'')把NULL转换成''; 2. 输入类型为整型,且允许为空的,可以使用COALESCE(inputP ...

  2. 关于lazyload的实现原理

    核心原理是: 1 设置一个定时器,计算每张图片是否会随着滚动条的滚动,而出现在视口(也就是浏览器中的 展现网站的空白部分 )中: 2 为<img>标签设置一个暂存图片URL的自定义属性(例 ...

  3. byte[] 解析、转码二三事

    1.先从byte 说起, byte 范围为 0~255 的整数,这个区间是在 int 范围中,所以 当byte 转为 int 时,则为小范围转大范围,隐式转换可以直接转换,反过来就是显式转换 需要Co ...

  4. Quartz.net定时任务框架的使用

    一:Nuget添加Quartz.net和Topshelf 二:新建HelloJob类继承IJob public class HelloJob : IJob       {            pub ...

  5. Collections.sort 的日期排序

    public static void main(String[] args) throws ParseException { // sort降序排列 List<Date> dates = ...

  6. php 把一个数组分成几个数组

    <?php /* * * 把一个数组分成几个数组 * $arr 数组 * $num 获取的数量 * */ function sliceArr($arr, $num) { //数组的个数 $lis ...

  7. 使用python玩跳一跳亲测使用步骤详解

    玩微信跳一跳,测测python跳一跳,顺便蹭一蹭热度: 参考博文 使用python玩跳一跳超详细使用教程 WIN10系统,安卓用户请直入此: python辅助作者github账号为:wangshub. ...

  8. 处理table 超出部分滚动问题

    我们有个需求是这样的,鉴于我的表达能力还是直接上原型图吧 今天主要记录上面的第四条解决过程. 首先我们的布局使用的table,当想给tbody设置高度的时候,发现不起作用.原因是table的默认是di ...

  9. js前端文件收集(一)

    1.保存cookies的文件: /** * Cookie plugin * * Copyright (c) 2006 Klaus Hartl (stilbuero.de) * Dual license ...

  10. ant常用的内置 task转自https://www.cnblogs.com/baicj/archive/2015/12/21/5063608.html

    ant 例如: <target name="callProjectB"> <echo message="In projectA calling proj ...