632. Binary Tree Maximum Node【Naive】
Find the maximum node in a binary tree, return the node.
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】的更多相关文章
- Binary Tree Maximum Node
Find the maximum node in a binary tree, return the node. Example Given a binary tree: 1 / \ -5 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 ...
- 【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 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum
题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
随机推荐
- OpenJudge 8782 乘积最大——S.B.S
8782:乘积最大 总时间限制: 1000ms 内存限制: 65536kB 描述 今年是国际数学联盟确定的“2000——世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江 ...
- @MySQL中length字符长度函数使用方法
MySQL里面的length函数是一个用来获取字符串长度的内置函数,一个汉字是算三个字符,中文的标点符号也是算三个字符,一个数字或字母算一个字符.具体用法示例如下: 1.查看某字符串的长度 SELEC ...
- iOS: 详细的正则表达式
一.简单的正则规则 1.由数字.26个英文字母或者下划线组成的字符串: ^[-9a-zA-Z_]{,}$ 2.非负整数(正整数 + 0 ): ^/d+$ 3. 正整数: ^[-]*[-][-]*$ 4 ...
- 高性能Mysql主从架构的复制原理及配置
1. 复制概述 1.1 mysql支持的复制类型 1.2 复制解决的问题 1.3 复制如何工作 2. 2 复制配置 2.1创建复制帐号 2.2拷贝数据 2.3配置master 2.4配置slave 2 ...
- 盗COOKIE之方法总结
1.xss跨站盗cookie 2.ajax跨域盗cookie 3.hosts文件映射 对于第一种方法,首先:在有跨站漏洞的页面贴上跨站代码如: <script>window.open('h ...
- C#文本文件(.txt)读写 [转]
目录 前言 读取txt文件 写入txt文件 前言 计算机在最初只支持ASCII编码,但是后来为了支持其他语言中的字符(比如汉字)以及一些特殊字符(比如€),就引入了Unicode字符集.基于Unico ...
- IP addr命令
我们都知道Windows上查看IP地址是ipconfig, Linux上是ifconfig,但是Linux上还有一个命令叫ip addr可以查看IP地址. 如上图所示命令显示了机器上的所有网卡,大部分 ...
- springmvc+jsp引用本地图片文件
1.图片文件路径,注意图片文件夹和WEB-INFO文件夹同级 2.web.xml配置 <servlet-mapping> <servlet-name>default</s ...
- 已阻止安装程序vs2015
Burn v3.7.4906.0, Windows v10.0 (Build 15063: Service Pack 0), path: H:\vs_enterprise.exe, cmdline: ...
- [Transducer] Step by Step to build a simple transducer
Transducers are composable algorithmic transformations. They are independent from the context of the ...