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 ...
随机推荐
- OpenCV特征点检测
特征点检测 目标 在本教程中,我们将涉及: 使用 FeatureDetector 接口来发现感兴趣点.特别地: 使用 SurfFeatureDetector 以及它的函数 detect 来实现检测过程 ...
- iOS:集合视图UICollectionView、集合视图控制器UICollectionViewController、集合视图单元格UICollectionViewCell(创建表格的另一种控件)
两种创建表格方式的比较:表格视图.集合视图(二者十分类似) <1>相同点: 表格视图:UITableView(位于storyboard中,通过UIViewController控制器实现 ...
- java设计模式1--工厂方法模式(Factory Method)
本文地址:http://www.cnblogs.com/archimedes/p/java-factory-method-pattern.html,转载请注明源地址. 工厂方法模式(别名:虚拟构造) ...
- hadoop中InputFormat 接口的设计与实现
InputFormat 主要用于描述输入数据的格式, 它提供以下两个功能.❑数据切分:按照某个策略将输入数据切分成若干个 split, 以便确定 Map Task 个数以及对应的 split.❑为 M ...
- Java最大的优势真的在于跨平台吗?
下面讨论仅仅针对PC端和移动端. 曾经是,但如今已经不是了. 有跨平台需求的仅仅是client应用.而不是服务端.比如桌面应用,你的客户可能是Windows用户.也可能是Linux用户,这时候假设不想 ...
- 【python】列出http://www.cnblogs.com/xiandedanteng/p/中的标题
# 列出http://www.cnblogs.com/xiandedanteng/p/中的标题 from bs4 import BeautifulSoup import requests user_a ...
- Html中的次方符号怎么写
代码: <p>1.01<sup>365</sup>=37.783,0.99<sup>365</sup>=0.025</p> 效果 ...
- [OpenGL红宝书]第一章 OpenGL概述
第一章 OpenGL概述 标签(空格分隔): OpenGL 第一章 OpenGL概述 1 什么是OpenGL 2 初识OpenGL程序 3 OpenGL语法 4 OpenGL渲染管线 41 准备向Op ...
- python常用代码积累
一.文件操作 1.判断一个目录是否存在,若不存在则创建 if not os.path.isdir(new_path): os.makedirs(new_path) 2.新建一个文件 f=open(&q ...
- STL - C++ 11的Lambda表达式(下)
关于lambda的基础知识,请参考上一篇的地址如下: http://www.cnblogs.com/davidgu/p/4825625.html 我们再举个STL使用Lambda来进行排序的例子,如下 ...