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);
}
}

最新文章

  1. 用Kotlin实现Android定制视图(KAD 06)
  2. UltralEdit 替换tips
  3. 每天一个linux命令--批处理
  4. Nhibernate Query By Criteria 条件查询
  5. 设计模式之美:Template Method(模板方法)
  6. 嵌入式Linux驱动学习之路(四)u-boot编译分析
  7. KMP,模式匹配算法
  8. &lt;Apache服务的搭建&quot;三件套&quot;《目录验证》《虚拟主机》《加密证书》&gt;
  9. jvm 漫谈 笔记
  10. HBase笔记--安装及启动过程中的问题
  11. 【POJ1005】I Think I Need a Houseboat
  12. Lucene学习之初步了解
  13. 【shell编程基础3】shell编程的组合应用之二:管道及其命令
  14. 八大排序算法---基于python
  15. 简单的OC程序
  16. Dynamics CRM2013 从subgrid中打开快速创建窗体创建数据
  17. MySQL8主从配置
  18. vue不通过路由直接获取url中参数的方法示例
  19. java指纹识别+谷歌图片识别技术_源代码
  20. 红外光通信装置数字部分思路点睛 2013年国赛f题

热门文章

  1. promise 和 setTimeout 在任务队列的执行顺序
  2. vuex中module的命名空间概念
  3. 解决Fiddler在win7系统下的安全证书问题
  4. 基础知识 Asp.Net MVC EF各版本区别
  5. [Pytorch Bug] &quot;EOFError: Ran out of input&quot; When using Dataloader with num_workers=x
  6. 【STM32H7教程】第27章 STM32H7的TCM,SRAM等五块内存的动态内存分配实现
  7. SQLServer某个库log日志过大,无法收缩日志文件 ,因为该文件结尾的逻辑日志文件正在使用
  8. SpringBoot系列之Spring容器添加组件方式
  9. ABAP对象-面向对象(转)
  10. python3 对list对象的增删改查