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 ...
随机推荐
- ecside 列表排序问题
ecside列表首先点击某一列排序,没有问题,再点第二列的时候没有起作用.原因是第二次排序的时候还包含着第一次排序的字段.所以排序始终是第一次的排序结果. 修改ecside.js 的ECSideUti ...
- cocos2d-x:将iOS项目编译成Andriod项目
来源:http://www.cnblogs.com/angzn/p/3328049.html 一.Android 环境搭建 1.安装Andriod-NDK(Native Development Kit ...
- LIBSVM与LIBLINEAR
对于多分类问题以及核函数的选取,以下经验规则可以借鉴: 如果如果特征数远远大于样本数的情况下,使用线性核就可以了. 如果特征数和样本数都很大,例如文档分类,一般使用线性核, LIBLINEAR比LIB ...
- [Firebase] 1. AngularFire, $save, $add and $remove, Forge
Basic angularFire options: $save, $add and $remove. The way connect firebase: var app = angular.modu ...
- UIWebView页面的控制(二)
1.UIWebView的内容控制的属性/方法列表 loading属性 确认当前页面是否在读入中 canGoForward属性 确认goForward 方法是否可运行, ...
- 截短字符串的函数(JS中适用)
function cutShort(str){ if(str.length>15){ str=str.substr(0,15)+"..."; } ...
- 使用javascript开发的视差滚动效果的云彩
在线演示 jquery.parallax.js是一款能够帮助你快速开发视差效果的jQuery插件,在这里我们使用它来开发一款漂亮的云朵视差效果. 主要代码: Javascript ........ 阅 ...
- 循环栅栏:CyclicBarrier(司令要求任务) 读书笔记
可以理解为循环栅栏,栅栏就是一种障碍物.假如我们将计数器设置为10,那么凑齐第一批10个线程后,计数器就会归零,然后接着凑齐下一批10个线程,这就是循环栅栏的含义. 构造器: public Cycli ...
- java使用链栈实现迷宫求解
java实现链栈在前面有所介绍:http://www.cnblogs.com/lixiaolun/p/4644141.html java实现链栈的代码: package stackapplicatio ...
- KineticJS教程(9)
KineticJS教程(9) 作者: ysm 9.选择器 Kinetic在舞台.层和组对象上都提供了get方法,用于返回这三者中包含的对象. 9.1.根据ID获取对象 要用id获取对象,首先要给对象 ...