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.

解法一:

 class Solution {
public:
/**
* @param root the root of binary tree
* @return the max node
*/
TreeNode* maxNode(TreeNode* root) {
// Write your code here
if (root == NULL) {
return root;
} TreeNode* left = maxNode(root->left);
TreeNode* right = maxNode(root->right); return max(root, max(left, right));
} TreeNode* max(TreeNode* a, TreeNode* b) {
if (a == NULL || b == NULL) {
return (a == NULL ? b : a);
} return (a->val > b->val ? a : b);
} };

632. Binary Tree Maximum Node【Naive】的更多相关文章

  1. Binary Tree Maximum Node

    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】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 ...

  4. [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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  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. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

随机推荐

  1. Integer判断相等,到底该用==还是equals

    在项目中涉及到整数判断的时候一直都是使用"=="进行判断的,但是用的时候心里老是在犯嘀咕,是不是应该使用equals呀?今天来看下这个问题! 在Object类中,equals方法的 ...

  2. JS中字符串的相关操作

    一.字符串的创建 创建一个字符串有几种方法.最简单的是用引号将一组字符包含起来,可以将其赋值给一个字符串变量. var myStr = "Hello, String!"; 可以用双 ...

  3. 设计模式(一)简单工厂(创建型)(Java&&PHP)

    面向对象设计的基本原则 单一职责系统中的每一个对象应该只有一个单独的职责,所有对象关注的应该是自身职责的完成. 基本思想:高内聚,低耦合. 开闭原则一个对象对扩展开放,对修改关闭.基本思想:对类的改动 ...

  4. Tensorflow 深度学习简介(自用)

    一些废话,也可能不是废话.可能对,也可能不对. 机器学习的定义:如果一个程序可以在任务T上,随着经验E的增加,效果P也可以随之增加,则称这个程序可以在经验中学习. “程序”指的是需要用到的机器学习算法 ...

  5. 如何在Django1.6结合Python3.4版本中使用MySql

    唉,最近赶了个新潮,用起了Python3.4跟Django1.6,数据库依然是互联网企业常见的MySql. 悲催的是在Python2.7时代连接MySql的MySQLdb还不支持Python3.4,还 ...

  6. Android开发者指南(9) —— ProGuard

    转: 前言 本章内容为开发者指南(Dev Guide)/Developing/Tools/ProGuard,本章内容为"混淆器",版本为Android3.0 r1,翻译来自:&qu ...

  7. Java取出String字符串括号中的内容

    形如: String idStr="dfda(2018)41324"; private int getId(String gSQL){ String quStr=gSQL.subs ...

  8. Caused by: java.lang.NumberFormatException: For input string: ""

    1.错误描写叙述 java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatEx ...

  9. Linux Samba安装与使用

    一 安装环境: 虚拟机:RedHat  5.4  192.168.75.128 主机环境:WIN7 32bit  192.168.75.1 网络连接方式:NAT 二 安装步骤: 说明: Ø  samb ...

  10. mysql表utf-8 字符串入库编码异常

    分析:http://www.myexception.cn/mysql/639943.html 解决方法:http://blog.sina.com.cn/s/blog_3f78232201011o26. ...