二叉树的右视图(199)

class Solution {
List<Integer> res = new ArrayList<>();
public List<Integer> rightSideView(TreeNode root) {
dfs(root, 0);
return res;
}
private void dfs(TreeNode node, int depth){
if (node == null) return; if (res.size() == depth){
res.add(node.val);
} dfs(node.right, depth+1);
dfs(node.left, depth+1);
}
}
  • 分析

因为一层只需要一个节点, 以depth作为限制

二叉树展开为链表(114)

class Solution {
public void flatten(TreeNode root) {
if (root == null) return; flatten(root.left);
flatten(root.right);
TreeNode lef = root.left;
TreeNode rig = root.right; root.left = null;
root.right = lef;
TreeNode curr = root; while (curr.right != null){
curr = curr.right;
}curr.right = rig; }
}
  • 分析

将左子树截断放到右端, 再将右子树放在左子树末尾

因为递归原因, 左子树必然为链表

从前序与中序遍历序列构造二叉树(105)

class Solution {
Map<Integer, Integer> map;
public TreeNode buildTree(int[] preorder, int[] inorder) {
int n = preorder.length;
map = new HashMap<>();
for (int i = 0; i < n; i++){
map.put(inorder[i], i);
} return dfs(preorder, 0, n, 0, n);
} private TreeNode dfs(int[] preorder, int lefPre, int rigPre, int lefIn, int rigIn){
if (lefPre == rigPre) return null; int lefSize = map.get(preorder[lefPre]) - lefIn;
TreeNode lefNode = dfs(preorder, lefPre+1, lefPre+1+lefSize, lefIn, lefIn+lefSize);
TreeNode rigNode = dfs(preorder, lefPre+1+lefSize, rigPre, lefIn+1+lefSize, rigIn);
return new TreeNode(preorder[lefPre], lefNode, rigNode);
}
}
  • 分析

以前序遍历取根, 切分中序遍历的左右子树作构造

路径总和(437)

class Solution {
Map<Long, Integer> map = new HashMap<>();
int res;
public int pathSum(TreeNode root, int targetSum) {
map.put(0L,1);
dfs(root, targetSum, 0L);
return res;
} private void dfs(TreeNode node, int targetSum, long subSum){
if (node == null) return;
subSum += node.val;
if (map.containsKey(subSum - targetSum)) res +=map.get(subSum - targetSum);
map.put(subSum, map.getOrDefault(subSum, 0)+1);
dfs(node.left, targetSum, subSum);
dfs(node.right, targetSum, subSum);
map.put(subSum, map.get(subSum)-1);
}
}
  • 分析

又是一个前缀和题目

二叉树的最近公共祖先(236)

class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null || p == root || q == root) return root; TreeNode lefNode = lowestCommonAncestor(root.left, p, q);
TreeNode rigNode = lowestCommonAncestor(root.right, p, q); if (lefNode == null && rigNode == null) return null;
if (lefNode != null && rigNode != null) return root; return lefNode != null ? lefNode : rigNode; }
}
  • 分析

找到子节点向上return, 两节点未相遇则继续向上return

  • 感悟

二叉树题目要注意需要返回给父节点的元素

二叉树中的最大路径和(124)

class Solution {
int res = Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
dfs(root);
return res;
}
private int dfs(TreeNode node){
if (node == null) return 0; int lefMax = Math.max(dfs(node.left), 0);
int rigMax = Math.max(dfs(node.right), 0); res = Math.max(res, lefMax+ node.val + rigMax);
return Math.max(lefMax, rigMax) + node.val;
}
}
  • 分析

以单向链作返回父节点元素, 取 左右链+节点 与记录的最大值作对比

hot100之二叉树下的更多相关文章

  1. leadcode的Hot100系列--二叉树创建和遍历

    很多题目涉及到二叉树,所以先把二叉树的一些基本的创建和遍历写一下,方便之后的本地代码调试. 为了方便,这里使用的数据为char类型数值,初始化数据使用一个数组. 因为这些东西比较简单,这里就不做过多详 ...

  2. [Leetcode] Binary tree level order traversal二叉树层次遍历

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  3. 【Ural1057】幂和的数量

    [题目描述] 写一个程序来计算区间[X,Y]内满足如下条件的整数个数:它恰好等于K个互不相等的B的整数幂之和. 举个例子.令X=15,Y=20,K=2,B=2.在这个例子中,区间[15,20]内有3个 ...

  4. Java实现二叉搜索树的添加,前序、后序、中序及层序遍历,求树的节点数,求树的最大值、最小值,查找等操作

    什么也不说了,直接上代码. 首先是节点类,大家都懂得 /** * 二叉树的节点类 * * @author HeYufan * * @param <T> */ class Node<T ...

  5. MTD设备驱动

    MTD(memory technology device):内存技术设备 是linux用于描述ROM,NAND,NOR等内存设备的子系统的抽象 MTD设备可以按块读写也可以按字节读写,也就是说MTD设 ...

  6. 考试总结T2(接上次整的T1)

    首先说一句,树的每个元素的名称的问题,(那个叫jie点的东西) 具体是节点还是结点...baidu百科写的是结点... 本文章将不考虑到底这俩字怎么写...所以两种都可能出现 T2描述: 扶苏翻遍了歌 ...

  7. leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)

    House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...

  8. STL常用序列容器

    这里简要的记述一下STL常用容器的实现原理,要点等内容. vector vector是比较常用的stl容器,用法与数组是非类似,其内部实现是连续空间分配,与数组的不同之处在于可弹性增加空间,而arra ...

  9. C++程序结构---1

    C++ 基础教程Beta 版 原作:Juan Soulié 翻译:Jing Xu (aqua) 英文原版 本教程根据Juan Soulie的英文版C++教程翻译并改编. 本版为最新校对版,尚未定稿.如 ...

  10. 剑指Offer面试题:21.从上到下打印二叉树

    一.题目:从上到下打印二叉树 题目:从上往下打印出二叉树的每个结点,同一层的结点按照从左到右的顺序打印.例如输入下图中的二叉树,则依次打印出8.6.10.5.7.9.11. 二叉树节点的定义如下,采用 ...

随机推荐

  1. 再谈MCP协议,看看 MCP 是如何重塑 AI 与外部数据源互动的能力?

    Techscribe Central 缩略图由 Techscribe Central 制作和编辑 MCP!!是不是一头雾水?我当时也是这个反应.我也是最近才听说它开始引发关注,然后我发现大多数人根本不 ...

  2. AI与.NET技术实操系列(九):总结篇 ── 探讨.NET 开发 AI 生态:工具、库与未来趋势

    1. 引言 本文作为本系列的最后一篇,旨在全面探讨 .NET 生态中与 AI 相关的工具.库.框架和资源,帮助开发者了解如何在 .NET 环境中开发 AI 应用.我们将分析 Microsoft 的 A ...

  3. study Rust-6【使用结构体组织相关联的数据】

    struc(structure) 定义并且实例化结构体: struct User { username: String, email: String, sign_in_count: u64, acti ...

  4. Log4j2 重大漏洞,编译好的log4j-2.15.0.jar包下载

    背景 12 月 10 日凌晨,Apache 开源项目 Log4j 的远程代码执行漏洞细节被公开,由于 Log4j 的广泛使用,该漏洞一旦被攻击者利用会造成严重危害.受本次漏洞影响的版本范围为Apach ...

  5. SpringBoot3整合SpringSecurity6(一)快速入门

    大家好,我是晓凡. 写在前面 不知道小伙伴们在学SpringSecurity过程中有没有和我一样的经历和烦恼. ①看完一篇文章或者一个教程,感觉学会了.但是一到实际项目中就不知道怎么用: ②被Spri ...

  6. 在 Go 语言中,构造一个并发安全的 map 集合

    Map 集合是 Go 中提供的一个 KV 结构的数据类型,对它的操作在实际的开发中应该是非常多的,不过它不是一个线程安全的. 1 .Map 不是线程安全的 编写下面的测试代码: func TestUn ...

  7. 备份一个迭代查找TreeViewItem的辅助函数

    private TreeViewItem FindTreeItem(TreeViewItem item, Func<TreeViewItem, bool> compare) { if (i ...

  8. DP学习总结

    动态规划是一种通过把原问题分解为相对简单的子问题的方式求解复杂问题的方法. -----OI Wiki 例.1-最大子段和 分析 DP四步 ⑴定义状态 定义\(dp_i\)表示以\(i\)结尾的最大子段 ...

  9. sonarqube+gitlab+jenkins+maven集成搭建(二)

    SonarQubeScanner 下载[root@localhost ~]# wget https://binaries.sonarsource.com/Distribution/sonar-scan ...

  10. EF core番外——EF core 输出生成的SQL 到控制台

    ----------------版权声明:本文为CSDN博主「爱睡觉的程序员」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.cs ...