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. #WEB安全基础 : HTTP协议 | 0x5 URI和URL

    URI(统一资源标识符)和URL(统一资源定位符)相信大家都知道URL吧,我们看看它们有什么区别 URI 长得就像这样 /images/hackr.jepg URL 长得像这样 http://hack ...

  2. 如何为 SpringMVC 编写单元测试:普通 Controller 测试(转)

    前一篇文章我们已经知道如何配置使用了 SpringMVC 测试框架的单元测试. 现在我们就该亲身实践下如何为普通 Controller 编写单元测试了. 接下来一个很明显的问题就是: 什么是普通 Co ...

  3. [macOS] PHP双版本,5.6跟7.1

    转过来的,原文看这里,https://www.symfony.fi/page/how-to-run-both-php-5-6-and-php-7-x-with-homebrew-on-os-x-wit ...

  4. sublime lincense for linux

    sublime lincense for linux Sublime Text 3.x (after Build 3133) —– BEGIN LICENSE —–TwitterInc200 User ...

  5. Python数据分析Numpy库方法简介(三)

    补充: np.ceil()向上取整 3.1向上取整是4 np.floor()向下取整 数组名.resize((m,n)) 重置行列 基础操作 np.random.randn()符合正态分布(钟行/高斯 ...

  6. CPU高速缓存

    目录 Code: 物理结构: 缓存行Cache Line 伪共享: 概念: 解决办法: 内存屏障: 理解: 参考: Code: public class Main { static long[][] ...

  7. datetime字符串中含T

    json序列化datetime类型,返回给前端进行展示,字符串带T 例如:var time = 2018-08-08T09:07:04.767  =>  time.substr(0, 16).r ...

  8. U3D外包公司—北京动点(公司性质)承接U3D、Kinect、VR虚拟现实,增强现实,体感互动,大屏互动等各类外包

    unity3d外包就找动点软件承接虚拟现实项目外包 承接U3D.Kinect.VR虚拟现实,增强现实,体感互动,大屏互动等各类外包 联系请加QQ:372900288 联系电话:13911652504 ...

  9. [原][工具][windows10安装][0x800F081F]windows 10 如何离线安装.net framework3.5 || 安装出现错误代码0x800F081F时如何解决

    参考解决方案:https://zhidao.baidu.com/question/1896669183365806820.html?fr=iks&word=win10%B0%B2%D7%B0+ ...

  10. LINUX介绍

    Linux操作系统被称为领先的服务器操作系统之一,它被普遍和广泛使用着.全球大约有数百款的Linux系统版本,每个系统版本都有自己的特性和目标人群. Linux的发行版本可以大体分为两类,一类是商业公 ...