<Tree> 298 250 366 199(高频) 98(高频)
298. Binary Tree Longest Consecutive Sequence
先序遍历,根左右。如果该节点的 value == 父节点value + 1, 则长度+1; 否则重置为1。
class Solution {
private int res = 0;
public int longestConsecutive(TreeNode root) {
if(root == null) return 0;
dfs(root, root.val, 0);
return res;
}
private void dfs(TreeNode root, int v, int out){
if(root == null) return;
if(root.val == v + 1){
out++;
}else{
out = 1;
}
res = Math.max(res, out);
dfs(root.left, root.val, out);
dfs(root.right, root.val, out);
}
}
250. Count Univalue Subtrees
后序遍历,左右根。
如果节点为叶子节点则 count++,并验证该节点是否与父节点的值相同,是则为true。如果节点和左右子树返回值都是true,则count++。
1.叶节点也都相同,count++。
2.对于当前遍历到的节点,如果对其左右子节点分别递归调用函数,返回均为true的话,那么说明当前节点的值和左右子树的值都相同,那么又多了一棵树,所以结果自增1
class Solution {
private int count;
public int countUnivalSubtrees(TreeNode root) {
count = 0;
helper(root, Integer.MAX_VALUE);
return count;
}
private boolean helper(TreeNode root, int v){
if(root == null) return true;
boolean left = helper(root.left, root.val);
boolean right = helper(root.right, root.val);
if(left && right){
count++;
return root.val == v;
}
return false;
}
}
366. Find Leaves of Binary Tree
一层一层剥离当前树的所有叶子节点。叶子节点高度设为0,所以 res.size( )的大小 == 当前层数 + 1。
每一个节点从左子节点和右子节点分开走可以得到两个深度,由于成为叶节点的条件是左右子节点都为空,所以我们取左右子节点中较大值加1为当前节点的深度值,知道了深度值就可以将节点值加入到结果res中的正确位置了。加入root.val时要remove该叶子节点 root = null。
class Solution {
public List<List<Integer>> findLeaves(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
height(root, res);
return res;
}
private int height(TreeNode root, List<List<Integer>> res){
if(root == null) return -1;
int level = Math.max(height(root.left, res), height(root.right, res)) + 1;
if(res.size() < level + 1){
res.add(new ArrayList<>());
}
res.get(level).add(root.val);
root = null;
return level;
}
}
199. Binary Tree Right Side View
求每一层最右边的节点
先序遍历中序遍历后序遍历都可,只要保证右侧节点的添加在左侧添加之后。
每当开始新一层节点的遍历之前,先把新一层最后一个节点值存到结果中
class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> res = new ArrayList<>();
HashMap<Integer, Integer> map = new HashMap<>();
int depth = 1;
dfs(root, depth, map);
//map --> List<Integer> list
depth = 1;
while(map.containsKey(depth)){
res.add(map.get(depth));
depth++;
}
return res;
}
private void dfs(TreeNode root, int depth, Map<Integer, Integer> map){
//Exit
if(root == null) return;
//traverse
map.put(depth, root.val); // do not have to be preorder
dfs(root.left, depth + 1, map);
dfs(root.right, depth + 1, map);
}
}
98. Validate Binary Search Tree
测试用例中可能有超过Integer.MAX_VALUE的值。故用Long.MAX_VALUE
class Solution {
public boolean isValidBST(TreeNode root) {
if(root == null) return true;
return valid(root, Long.MIN_VALUE, Long.MAX_VALUE);
}
private boolean valid(TreeNode root, long low, long high){
if(root == null) return true;
if(root.val <= low || root.val >= high) return false;
return valid(root.left, low, Math.min(root.val, high)) && valid(root.right, Math.max(low, root.val), high);
}
}
<Tree> 298 250 366 199(高频) 98(高频)的更多相关文章
- 用 R 进行高频金融数据分析简介
作者:李洪成 摘自:http://cos.name/wp-content/uploads/2013/11/ChinaR2013SH_Nov03_04_LiHongcheng.pdf 高频数据 金融市场 ...
- 高频交易[z]
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:袁浩瀚链接:https://www.zhihu.com/question/21789812/answer/22178178来源 ...
- Java才是世界上最好的语言,Java在高频交易中替代C++
高频交易 高频交易是指从那些人们无法利用的极为短暂的市场变化中寻求获利的计算机化交易,比如,某种证券买入价和卖出价差价的微小变化,或者某只股票在不同交易所之间的微小价差.在高频交易中,自动化应用程序每 ...
- Leetcode重点 250题-前400 题
删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- Fast Matrix Operations(UVA)11992
UVA 11992 - Fast Matrix Operations 给定一个r*c(r<=20,r*c<=1e6)的矩阵,其元素都是0,现在对其子矩阵进行操作. 1 x1 y1 x2 y ...
- poj 1018 Communication System
点击打开链接 Communication System Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21007 Acc ...
- SVG:中国地图
中国地图 <svg height="578" version="1.1" width="718" xmlns="http:/ ...
- AWR Report 关键参数详细分析
WORKLOAD REPOSITORY report for DB Name DB Id Instance Inst num Startup Time Release RAC CALLDB 12510 ...
随机推荐
- 《icra16_slam_tutorial_tardos.pdf》
icra16_slam_tutorial_tardos.pdf EKF: https://www.cnblogs.com/gaoxiang12/p/5560360.html 7. 小结 卡尔曼滤波是递 ...
- 将静态页面部署到github.io
背景: 我的腾讯云服务器是之前利用学生身份(有优惠)买的,现在快到期了,而且服务器上面只有一个引导页(静态页面)还有用,别的项目都没有用了.所以就想找一种不花钱买服务器就可以访问到我的引导页的方法 ...
- golang数据结构之双链表
目录结构: doubleLink.go package link import ( "fmt" ) //HerosNode 链表节点 type HerosNode struct { ...
- mysql压缩备份导入导出
mysqldump工具自带选项没有对导出备份文件压缩功能,可结合gzip只使用一条命令压缩导出文件,方法如下: mysqldump压缩导出:# mysqldump -h192.168.0.3 -P33 ...
- Abp vNext框架 从空项目开始 使用ASP.NET Core Web Application-笔记
参考 Abp vNext框架 从空项目开始 使用ASP.NET Core Web Application http://www.vnfan.com/helinbin/d/745b1e040c9b4f6 ...
- c#微信公众号开发一----基本设置,服务器配置token验证,获取timestamp/nonce/signature
一.c#微信公众号开发----基本设置 参考微信官方文档 https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Acce ...
- JS实现根据两点位置的经纬度获取距离
// 经纬度转换成三角函数中度分表形式. function rad(d) { return d * Math.PI / 180.0; } // 根据经纬度计算距离,参数分别为第一点的纬度,经度:第二点 ...
- java基础(28):数据库、表及表数据、SQL语句
1. 数据库 1.1 数据库概述 什么是数据库 数据库就是存储数据的仓库,其本质是一个文件系统,数据按照特定的格式将数据存储起来,用户可以对数据库中的数据进行增加,修改,删除及查询操作. 什么是数据库 ...
- Java的23种设计模式,详细讲解(二)
本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...
- idea的service注入mapper报错
一.问题 idea的java项目中,service类中注入mapper报错 二.解决 方法1 在mapper类上加上 @Repository 注解即可,当然不加也行,程序也不回报错,是idea的误报 ...