基础算法和数据结构高频题 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见:算法与数 ...
随机推荐
- ZooKeeper学习第四期---构建ZooKeeper应用(转)
转载来源:https://www.cnblogs.com/sunddenly/p/4064992.html 一.配置服务 配置服务是分布式应用所需要的基本服务之一,它使集群中的机器可以共享配置信息中那 ...
- 为mysql数据备份建立最小权限的用户
mysqldump 备份所需要的最小权限说明: 1.对于table,mysqldump 最少要有select权限 2.如果要产生一份一致的备份,mysqldump 要有lock tables权限 3. ...
- 零基础配置Hadoop集群——Ambari安装及配置详解
1. 准备工作 1.1. 系统环境 主机列表: IP地址 HostName 描述 192.168.610.153 ambari.server Ambari服务器 192.168.10.152 had ...
- 读书笔记——《谁说菜鸟不会数据分析—Python篇》
最近刚读完一本新书,关注的公众号作者出的“谁说菜鸟不会数据分析—Python篇”,话说现在很多微信公众号大牛都在出书,这貌似是一个趋势.. 说说这本书吧,我之前看过一些网文,对于数据分析这一块也有过一 ...
- composer简述
1.composer是一个php依赖管理工具,而不是一个包管理器.怎么来理解呢?就像在是在电脑中安装了个电脑管家,在电脑管家的软件管理中下载和更新软件,其实这个电脑管家只是一个管理工具,而真正的软件可 ...
- zabbix自定义监控项数据类型错误
问题描述 监控cpu使用率,脚本获取的值是浮点型 zabbix创建监控项时没有选数据类型,导致监控数据有问题. 查看 zabbix-server 日志: ::203016.768 error rea ...
- CSS3文本与字体
一.CSS3 换行 1.word-break(规定自动换行的处理方法) word-break: normal / break-all / keep-all; /* normal:使用浏览器默认的换行规 ...
- Appium+python自动化(十五)- Android 这些基础知识,你知多少???(超详解)
简介 前边具体操作和实战已经讲解和分享了很多了,但是一些android的一些基础知识,你又知道多少了,你都掌握了吗?这篇就由宏哥给小伙伴们既是一个分享,又是对前边的一次总结.为什么要对这些做一个简单的 ...
- Java并发框架:Executor
介绍 随着当今处理器中可用的核心数量的增加, 随着对实现更高吞吐量的需求的不断增长,多线程 API 变得非常流行. Java 提供了自己的多线程框架,称为 Executor 框架. 1. Execut ...
- Codeforces Gym100502H:Clock Pictures(KMP算法)
http://codeforces.com/gym/100502/attachments 题意:有两个时钟上面有n个指针,给出的数字代表指针的角度.问能否在某一时刻使得两个时钟的指针重合. 思路:容易 ...