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. Kubernetes有状态应用管理——PetSet

    目录贴:Kubernetes学习系列 1.介绍 在Kubernetes中,大多数的Pod管理都是基于无状态.一次性的理念.例如Replication Controller,它只是简单的保证可提供服务的 ...

  2. Maven setting.xml文件详解(转)

    maven的配置文件settings.xml存在于两个地方: 1.安装的地方:${M2_HOME}/conf/settings.xml 2.用户的目录:${user.home}/.m2/setting ...

  3. h5 的localStorage和sessionStorage存到缓存里面的值是string类型

    localStorage永久存在,不手动清除永远存在:sessionStorage 一次会话的浏览器关闭就自动清除 h5 的localStorage和sessionStorage 存到缓存里面的值都是 ...

  4. 使用日期插件用js处理日期格式

    function compareDate(checkStartDate, checkEndDate) {    var arys1= new Array();    var arys2= new Ar ...

  5. SSH服务理论+实践

    1)远程管理服务知识介绍 SSH远程登录服务介绍说明 01. SSH-Secure Shell Protocol 安全加密shel协议 SSH远程登录服务功能作用 01. 提供类似telnet远程登录 ...

  6. python爬虫——论抓包的正确姿势和学好Javascript的重要性(1)

    没事想爬下数据,就入了scrapy坑,跟着https://zhuanlan.zhihu.com/data-factory这篇教程走,中间被小数量的网站坑过,不过还是写出了爬虫~~ 切糕王子:毫无防御, ...

  7. Python xml模块

    xml模块 自己创建xml文档 import xml.etree.cElementTree as ET new_xml = ET.Element("personinfolist") ...

  8. 【mysql】逗号分割字段的行列转换

    由于很多业务表因为历史原因或者性能原因,都使用了违反第一范式的设计模式,即同一个列中存储了多个属性值.这种模式下,应用常常需要将这个列依据分隔符进行分割,并得到列转行的结果:这里使用substring ...

  9. [C++ Primer Plus] 第5章、循环和关系表达式(二)课后习题

    1.编写一个要求用户输入两个整数的程序,将程序将计算并输出这两个整数之间(包括这两个整数)所有的整数的和.这里假设先输入较小的整数,例如如果用户输入的是2和9,则程序将指出2-9之间所有整数的和为44 ...

  10. SSM-网站后台管理系统制作(2)---SSM基本工作原理

    SSM基本工作原理 讲解网站:https://www.w3cschool.cn/wkspring/dcu91icn.html 构建基本工作环境: mysql eclipse(tomcat8.0) Hb ...