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. Weighted Quick Union

    Weighted Quick Union即: 在Quick Union的基础上对结点加权(weighted),在parent[i]基础上增加一个size[i]. 用来存储该结点(site)的所有子结点 ...

  2. Spring 学习——Spring AOP——AOP概念篇

    AOP AOP的定义:AOP,Aspect Oriented Programming的缩写,意为面向切面编程,是通过预编译或运行期动态代理实现程序功能处理的统一维护的一种技术 实现方式 预编译 Asp ...

  3. Git的初次使用

    一.配置本地Git库 1.下载安装好Git,并配置自己的信息. git config --global user.name"yourname"配置你的名称 git config - ...

  4. websocket是如何进行建立连接与通信的?(简单理解)

    握手过程: websocket-client端通过ws协议向websocket-server端发起连接请求前,首先在自己的请求头中添加Sec-Websocket-Key键值对,值为根据自己账号通过一定 ...

  5. 一键清空Form表单数据

    今天在工作项目调试bug当中,遇到这样的需求:页面上的数据太多,一个一个清空太繁琐,所以就采用全部清空的写法: $(':input','#myform').not(':button, :submit, ...

  6. hdu 1895 Sum Zero hash

    Sum Zero Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Proble ...

  7. spring 事务的七中传播行为五中隔离

    事务的传播行为(七): public enum Propagation { REQUIRED(TransactionDefinition.PROPAGATION_REQUIRED), SUPPORTS ...

  8. 微信小程序,加载更多

    html <!-- 头部 --> <view class='tab'> <view class="tab-new {{selected_new?'active' ...

  9. html5的websocket

    转载:http://blog.csdn.net/liuhe688/article/details/50496780 var WebSocketServer = require('ws').Server ...

  10. ubuntu1404安装搜狗输入法

    1.安装fcitx,一种输入法框架 apt-get install fcitx 2.配置使用fcitx 配置中心-语言支持-键盘输入方式系统,选择fcitx 3.登出再登入 4.下载sougou安装d ...