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(高频)的更多相关文章

  1. 用 R 进行高频金融数据分析简介

    作者:李洪成 摘自:http://cos.name/wp-content/uploads/2013/11/ChinaR2013SH_Nov03_04_LiHongcheng.pdf 高频数据 金融市场 ...

  2. 高频交易[z]

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:袁浩瀚链接:https://www.zhihu.com/question/21789812/answer/22178178来源 ...

  3. Java才是世界上最好的语言,Java在高频交易中替代C++

    高频交易 高频交易是指从那些人们无法利用的极为短暂的市场变化中寻求获利的计算机化交易,比如,某种证券买入价和卖出价差价的微小变化,或者某只股票在不同交易所之间的微小价差.在高频交易中,自动化应用程序每 ...

  4. Leetcode重点 250题-前400 题

    删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & ...

  5. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  6. Fast Matrix Operations(UVA)11992

    UVA 11992 - Fast Matrix Operations 给定一个r*c(r<=20,r*c<=1e6)的矩阵,其元素都是0,现在对其子矩阵进行操作. 1 x1 y1 x2 y ...

  7. poj 1018 Communication System

    点击打开链接 Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21007   Acc ...

  8. SVG:中国地图

    中国地图 <svg height="578" version="1.1" width="718" xmlns="http:/ ...

  9. AWR Report 关键参数详细分析

    WORKLOAD REPOSITORY report for DB Name DB Id Instance Inst num Startup Time Release RAC CALLDB 12510 ...

随机推荐

  1. luoguP3649 [APIO2014]回文串

    题意 关于回文自动机的讲解见这里 由于回文串个数是\(O(n)\)的,直接回文自动机上统计并比较即可. code: #include<bits/stdc++.h> using namesp ...

  2. Make Them Odd

    time limit per test3 secondsmemory limit per test256 megabytesinput: standard inputoutput: standard ...

  3. Codeforces Round #594 (Div. 2) B. Grow The Tree 水题

    B. Grow The Tree Gardener Alexey teaches competitive programming to high school students. To congrat ...

  4. pytorch固定部分参数

    pytorch固定部分参数 不用梯度 如果是Variable,则可以初始化时指定 j = Variable(torch.randn(5,5), requires_grad=True) 但是如果是m = ...

  5. 手摸手教你bootstrap定制

    老实说我一直不太喜欢使用bootstrap,bootstrap样式组件虽然丰富但实际开发使用到的不多:栅格系统虽然好用,满屏div也是看的头疼:所以当经理说要用bootstrap开发新项目的时候,我内 ...

  6. Map拼接URL地址

    import java.util.HashMap; import java.util.Iterator; import java.util.Map; /** * @Author: hoje * Des ...

  7. 利用内存锁定技术防止CE修改

    利用内存锁定技术防止CE修改 通过这种在R3环利用的技术,我们可以来达到保护内存的目的,像VirtualProtect等函数来修改页属性根本无法修改. 而CE修改器推测应该使用VirtualProte ...

  8. c# 字符串中全角和半角字符互转

    public class ConvertDBCAndSBC { /// <summary>半角转成全角 /// 半角空格32,全角空格12288 /// 其他字符半角33~126,其他字符 ...

  9. The method newInstance() from the type Class is deprecated since version 9

    newInstance()在 java9中已被弃用 JAVA9之前用法 Class.forName("类的全限定名").newInstance(); JAVA9之后用法 Class ...

  10. JS基础语法---分支语句之:switch-case语句---3个练习

    switch-case语句---分支语句---多分支语句 语法: switch(表达式){ case 值1:代码1;break; case 值2:代码2;break; case 值3:代码3;brea ...