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. Istio在Openshift 3.11的安装

    详细安装步骤及解释参考 https://docs.openshift.com/container-platform/3.11/servicemesh-install/servicemesh-insta ...

  2. Hibernate 实体关联关系映射(转载)

    原文链接地址:http://lavasoft.blog.51cto.com/62575/39398/ Hibernate:Hibernate关联关系映射实例速查   Hibernate关联关系映射目录 ...

  3. C++实现委托机制(二)

    1.引言: 上一篇文章已经介绍了如何构建一个无参数无返回值的函数指针的委托,这篇文章将对上一文章所述委托进行扩展,使得可以注册任意函数指针,不过再讲篇内容之前先要介绍一下实现这个功能所需要了解的C++ ...

  4. 使用ReportStudio打开cube模型创建报表出现两个最细粒度名称

    本人也是第一次遇到这样的问题,此问题甚是简单,也许很简短的一句话就可以解决这个问题了,看官请留神哦 cube做好发布到cognos之后使用Analysis Studio打开结构正常 于是想到要用此数据 ...

  5. html5-video视频播放

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...

  6. function/bind

    1.函数指针指向一类函数,这类函数的类型一样,也就是函数的返回类型和形参表一样. 2.不同的函数类型要使用不同的函数指针,才能指向它,有没有好的办法呢? 类比思考下,交换方法,对不同的类型要写不同的s ...

  7. Citrix服务器虚拟化之二十九 XenApp 6.5发布服务器上的应用程序

    Citrix服务器虚拟化之二十九  XenApp 6.5发布服务器上的应用程序 XenApp可发布以下类型的资源向用户提供信息访问,这些资源可在服务器或桌面上虚拟化: 1)  服务器桌面:发布场中服务 ...

  8. IDEA20181.4破解

    1.下载破解补丁: http://idea.lanyus.com/jar/JetbrainsIdesCrack-4.2-release-sha1-3323d5d0b82e716609808090d3d ...

  9. 【转】web服务器工作原理

    一.静态网页的工作原理如下:A.用户在浏览器的地址栏输入要访问的地址并回车,触发这个浏览请求. B.浏览器将请求发送到Web服务器.C.Web服务器接受这个请求,并根据请求文件的后缀名判定是否为HTM ...

  10. @SuppressWarnings 参数列表信息