基础算法和数据结构高频题 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见:算法与数 ...
随机推荐
- 30211Java_类
类 类:我们叫做class. 对象:叫做Object,instance(实例).某个类的对象,某个类的实例.是一样的意思.1.对象是具体的事物;类是对对象的抽象;2.类可以看成一类对象的模板,对象可以 ...
- 案例解析:springboot自动配置未生效问题定位(条件断点)
Spring Boot在为开发人员提供更高层次的封装,进而提高开发效率的同时,也为出现问题时如何进行定位带来了一定复杂性与难度.但Spring Boot同时又提供了一些诊断工具来辅助开发与分析,如sp ...
- Redis 学习笔记(篇一):字符串和链表
本次学习除了基本内容之外主要思考三个问题:why(为什么).what(原理是什么).which(同类中还有哪些类似的东西,相比有什么区别). 由于我对 java 比较熟悉,并且 java 中也有字符串 ...
- windows下nginx的安装和使用
LNMP的安装与配置 windows下的nginx安装和使用 1.1 去官网下载相应的安装包:http://nginx.org/en/download.html 1.2 解压后进入PowerShell ...
- VsCode 快捷键(Mac)
按键使用符号: Shift (⇧) Control(或 Ctrl)⌃ Command(或 Cmd)⌘ Option(或 Alt)⌥ 打开文件夹 Cmd+o 调试 // 开启调试 F5 // 停止调试 ...
- docker安装gitlab-runner
docker安装gitlab-runner docker pull gitlab/gitlab-runner:latest安装gitlab-runner 打开自己搭建的GitLab网站,点击顶栏的Sn ...
- 爱,死亡和机器人(Love,Death&Robots)
从我自己的角度来讲,我真的是很喜欢这部短片,奇幻,科幻,喜剧交叉在一起构成了这18部短片.精彩绝伦,我只能这么去形容. 但是有没有不足呢?客观的来说,也存在不足,过度的吹捧使得有些人神话了它,认为立意 ...
- Coderforces 633D:Fibonacci-ish(map+暴力枚举)
http://codeforces.com/problemset/problem/633/D D. Fibonacci-ish Yash has recently learnt about the ...
- Java学习笔记之---集合
Java学习笔记之---集合 (一)集合框架的体系结构 (二)List(列表) (1)特性 1.List中的元素是有序并且可以重复的,成为序列 2.List可以精确的控制每个元素的插入位置,并且可以删 ...
- J2EE:Servlet上传文件到服务器,并相应显示
Servlet 可以与HTML一起使用来允许用户上传文件到服务器 编辑上传文件的页面upload.html 注意事项:上传方式使用POST不能使用GET(GET不能上传文件) 表单 enctype 属 ...