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. excel 快捷使用

    1)excel填充相同内容 A:在需要填充相同内容的格中一个添加需要的内容,然后选中所有填充格,快捷键 ctrl-D 2)excel中合并单元格快捷键 A:1.点击上方-其他命令 2.选择“合并”进行 ...

  2. (笔试题)关于C++的虚函数和多态性

    以下两段程序的输出是什么? 程序1: #include "stdio.h" class Base { public: int Bar(char x) { return (int)( ...

  3. C#基础视频教程5.2 如何编写简单的超级热键

    我们前面已经理解了如何使用官方的代码实现鼠标键盘的监控,其实还差一点,因为他的代码只能捕捉单个的按键,而其实我们要的是组合键.什么是组合键呢?比如我想定义同时按下WIN+C是去执行屏幕截图.这只要理解 ...

  4. ASP站点无法访问怎么办

    确保启用了目录浏览功能

  5. Simple TCP/IP Echo Server & Client Application in C#

    1. TCP Server The server’s job is to set up an endpoint for clients to connect to and passively wait ...

  6. Tomcat 之 启动tomcat时 错误: 代理抛出异常 : java.rmi.server.ExportException: Port already in use: 1099;

    错误: 代理抛出异常 : java.rmi.server.ExportException: Port already in use: 1099; nested exception is:  java. ...

  7. linux系统中的DNS服务器介绍

    http://lq2419.blog.51cto.com/1365130/1172269 DNS:Domain Name Service,linux上的DNS服务是基于一种软件BIND实现的.BIND ...

  8. 页面刷新 vuex 数据重新被初始化

    1.原因 vuex里用来存储的也只是一个全局变量,当页面刷新,该全局变量自然不存在了. 2.解决 使用localStorage存储一份 (1)storage.js /** * vuex localSt ...

  9. java线程--volatile实现可见性

    volatile关键字: 1)能够保证volatile变量的可见性 2)不能保证volatile变量复杂操作的原子性. volatile如何实现内存可见性: 深入来说:通过加入内存屏障和禁止重排序优化 ...

  10. KVM虚拟化学习笔记系列文章列表(转)

    Kernel-based Virtual Machine KVM虚拟化学习笔记系列文章列表----------------------------------------kvm虚拟化学习笔记(一)之k ...