基础算法和数据结构高频题 II
DFS的两种理解方式:
1. 按照实际执行顺序模拟 (适合枚举型DFS,下节课内容)
2. 按照DFS的定义宏观理解 (适合分治型DFS,本节课内容)
1 Convert BST to Greater Tree
int sum = ;
public TreeNode convertBST(TreeNode root) {
dfs(root);
return root;
}
void dfs(TreeNode root) {
if (root == null) {
return;
}
dfs(root.right);
sum = sum + root.val;
root.val = sum;
dfs(root.left);
}
2 Inorder Successor in Binary Search Tree
public TreeNode InorderSuccessor(TreeNode root, TreeNode p) {
if (root == null || p == null) {
return null;
}
if (root.val <= p.val) {
return InorderSuccessor(root.right, p);
} else {
TreeNode left = InorderSuccessor(root.left, p);
return left == null ? root, left;
}
}
3 Validate Binary Search Tree
public boolean isValidBST(TreeNode root)
{
return help(root, Long.MIN_VALUE, Long.MAX_VALUE);
}
boolean help(TreeNode root, long min, long max) {
if (root == null) {
return true;
}
if (root.val <= min || root.val >= max) {
return false;
}
return help(root.left, min, root.val) && help(root.right, root.val, max);
}
4Binary Tree Inorder Traversal
public class Solution {
/**
* @param root: The root of binary tree.
* @return: Inorder in ArrayList which contains node values.
*/
public ArrayList<Integer> inorderTraversal(TreeNode root) {
// write your code here
ArrayList<Integer> res = new ArrayList<>();
dfs(root, res);
return res;
}
void dfs(TreeNode root, ArrayList<Integer> res) {
if (root == null) {
return;
}
dfs(root.left, res);
res.add(root.val);
dfs(root.right, res);
}
}
二叉树类问题
5 Binary Tree Flipping
TreeNode newRoot;
void dfs(TreeNode cur) {
if (cur.left == null) {
newRoot = cur;
return;
}
dfs(cur.left);
cur.left.right = cur;
cur.left.left = cur.left;
cur.left = null;
cur.right = null;
} public TreeNodee upsideDownBinaryTree(TreeNode root) {
if (root == null) {
return root;
}
dfs(root);
return newRoot;
}
6 Binary Tree Leaves Order Traversal
Map<Integer, List<Integer>> map = new HashMap<>();
int dfs(TreeNode root) {
if (root == null) {
return ;
}
int left = dfs(root.left);
int right = dfs(root.right);
int max = Math.max(left, right) + ;
if (!map.containsKey(max)) {
map.put(max, new ArrayList<>());
}
map.get(max).add(root.val);
return max;
} public List<List<Integer>> findLeaves(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) {
return res;
}
int max_deep = dfs(root);
for (int i = ; i <= max_deep; i++) {
res.add(map.get(i));
}
return res;
}
7Binary Tree Leaves Order Traversal
public List<List<Integer>> virtalOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) {
return res;
}
Map<Integer, ArrayList<Integer>> map = new HashMap<>();
Queue<Integer> c = new LinkedList<>();
Queue<TreeNode> q = new LinkedList<>();
c.offer();
q.offer(root);
while (!q.isEmpty()) {
Integer l = c.poll();
TreeNode node = q.poll();
if (!map.containsKey(l)) {
map.put(l, new ArrayList<>());
}
map.get(l).add(node.val);
if (node.left != null) {
c.offer(l - );
q.offer(node.left);
}
if (node.right != null) {
c.offer(l + );
q.offer(node.right);
}
}
for (int i = Collections.min(map.keySet()); i <= Collections.max(map.keySet()); i++) {
res.add(map.get(i));
}
return res;
}
基础算法和数据结构高频题 II的更多相关文章
- 第三章 基础算法和数据结构高频题 I
区间类问题 1 Missing Interval public List<String> findMissingRanges(int[] nums, int lower, int uppe ...
- 算法与数据结构实验题6.4 order (二叉树)
1.题目: 2.代码: #include<iostream> #include<algorithm> using namespace std; struct Node { in ...
- 算法与数据结构实验题 5.2 Missile
1.题目: 2.解题思路: 把每个点对应的两条半径求出,之后对d1进行升序排序,对应d2也改变位置.其中一个圆心的半径r1确定之后,除去第一个圆包围的点,在其余点中找到另外一个圆的最长的半径r2,此时 ...
- 算法与数据结构实验题 6.4 Summary
★实验任务 可怜的 Bibi 丢了好几台手机以后,看谁都像是小偷,他已经在小本本上记 下了他认为的各个地点的小偷数量. 现在我们将 Bibi 的家附近的地形抽象成一棵有根树.每个地点都是树上的 一个节 ...
- 算法与数据结构实验题 6.3 search
★实验任务 可怜的 Bibi 刚刚回到家,就发现自己的手机丢了,现在他决定回头去搜索 自己的手机. 现在我们假设 Bibi 的家位于一棵二叉树的根部.在 Bibi 的心中,每个节点 都有一个权值 x, ...
- 算法与数据结构实验题 4.2 小 F 打怪
★实验任务 小 F 很爱打怪,今天因为系统 bug,他提前得知了 n 只怪的出现顺序以及击 倒每只怪得到的成就值 ai.设第一只怪出现的时间为第 1 秒,这个游戏每过 1 秒 钟出现一只新怪且没被击倒 ...
- 算法与数据结构实验题 4.1 伊姐姐数字 game
★实验任务 伊姐姐热衷于各类数字游戏,24 点.2048.数独等轻轻松松毫无压力.一 日,可爱的小姐姐邀请伊姐姐一起玩一种简单的数字 game,游戏规则如下: 一开始桌上放着 n 张数字卡片,从左到右 ...
- 算法与数据结构基础 - 堆(Heap)和优先级队列(Priority queue)
堆基础 堆(Heap)是具有这样性质的数据结构:1/完全二叉树 2/所有节点的值大于等于(或小于等于)子节点的值: 图片来源:这里 堆可以用数组存储,插入.删除会触发节点shift_down.shif ...
- 算法与数据结构基础 - 广度优先搜索(BFS)
BFS基础 广度优先搜索(Breadth First Search)用于按离始节点距离.由近到远渐次访问图的节点,可视化BFS 通常使用队列(queue)结构模拟BFS过程,关于queue见:算法与数 ...
随机推荐
- F4帮助
在INITIALIZATION之后添加 AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_month-low 事件,s_month-low为要添加的搜索帮助. 下面 ...
- OSI参考模型---网络基础篇(1)
什么是网络 网络就是将分布在不同地理位置,具有独立功能的终端(一切联网的设备都叫终端:例如电脑,手机,智能家电等等联网的设备),通过通信线路(双绞线.光纤.电话线等等)和通信设备(例如:交换机.路由器 ...
- 农夫过河 (BFS)(队列)
1 .问题描述 要求设计实现农夫过河问题(农夫带着一只狼,一只养,一棵白菜,一次只能带一个东西)如何安全过河. 2 .问题的解决方案: 可以用栈与队列.深度优先搜索算法及广度优先搜索算法相应的原理去解 ...
- Java基础中字符串与字符的注意点!
在Java中,字符的表达与字符串的表达是不一样的!话不多说,直接上代码!!! String a="a"; char a='a'; 其中他们的引号是不一样的
- Hive入门(四)查询优化
1 本地模式 0.7版本后Hive开始支持任务执行选择本地模式(local mode). 大多数的Hadoop job是需要hadoop提供的完整的可扩展性来处理大数据的.不过,有时hive的输入数据 ...
- RedisDesktopManager远程连接Linux系统的Redis服务
linux下安装redis :https://www.runoob.com/redis/redis-install.html 进入 src 运行redis : ./redis-server 打开另 ...
- django基础知识之定义模板:
定义模板 模板语言包括 变量 标签 { % 代码块 % } 过滤器 注释{# 代码或html #} 变量 语法: {{ variable }} 当模版引擎遇到一个变量,将计算这个变量,然后将结果输出 ...
- JSON.stringify() 的深入理解
目录 序言 语法 深入理解 序言 最近在看<你所不知道的javascript>[中卷]一书,第一部分是类型和语法.本文是基于这部分的产物.在强制类型转换->抽象值操作-> to ...
- pdfminer获取每页的layout
#! python2 # coding: utf-8 import sys from pdfminer import pdfparser from pdfminer import pdfdocumen ...
- 修改Windows10的host文件。
一.Windows10中host地址. c:\windows\system32\drivers\etc\hosts 其他系统中的位置. Windows操作系统(Windows XP/7/8/10): ...