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的更多相关文章

  1. 第三章 基础算法和数据结构高频题 I

    区间类问题 1 Missing Interval public List<String> findMissingRanges(int[] nums, int lower, int uppe ...

  2. 算法与数据结构实验题6.4 order (二叉树)

    1.题目: 2.代码: #include<iostream> #include<algorithm> using namespace std; struct Node { in ...

  3. 算法与数据结构实验题 5.2 Missile

    1.题目: 2.解题思路: 把每个点对应的两条半径求出,之后对d1进行升序排序,对应d2也改变位置.其中一个圆心的半径r1确定之后,除去第一个圆包围的点,在其余点中找到另外一个圆的最长的半径r2,此时 ...

  4. 算法与数据结构实验题 6.4 Summary

    ★实验任务 可怜的 Bibi 丢了好几台手机以后,看谁都像是小偷,他已经在小本本上记 下了他认为的各个地点的小偷数量. 现在我们将 Bibi 的家附近的地形抽象成一棵有根树.每个地点都是树上的 一个节 ...

  5. 算法与数据结构实验题 6.3 search

    ★实验任务 可怜的 Bibi 刚刚回到家,就发现自己的手机丢了,现在他决定回头去搜索 自己的手机. 现在我们假设 Bibi 的家位于一棵二叉树的根部.在 Bibi 的心中,每个节点 都有一个权值 x, ...

  6. 算法与数据结构实验题 4.2 小 F 打怪

    ★实验任务 小 F 很爱打怪,今天因为系统 bug,他提前得知了 n 只怪的出现顺序以及击 倒每只怪得到的成就值 ai.设第一只怪出现的时间为第 1 秒,这个游戏每过 1 秒 钟出现一只新怪且没被击倒 ...

  7. 算法与数据结构实验题 4.1 伊姐姐数字 game

    ★实验任务 伊姐姐热衷于各类数字游戏,24 点.2048.数独等轻轻松松毫无压力.一 日,可爱的小姐姐邀请伊姐姐一起玩一种简单的数字 game,游戏规则如下: 一开始桌上放着 n 张数字卡片,从左到右 ...

  8. 算法与数据结构基础 - 堆(Heap)和优先级队列(Priority queue)

    堆基础 堆(Heap)是具有这样性质的数据结构:1/完全二叉树 2/所有节点的值大于等于(或小于等于)子节点的值: 图片来源:这里 堆可以用数组存储,插入.删除会触发节点shift_down.shif ...

  9. 算法与数据结构基础 - 广度优先搜索(BFS)

    BFS基础 广度优先搜索(Breadth First Search)用于按离始节点距离.由近到远渐次访问图的节点,可视化BFS 通常使用队列(queue)结构模拟BFS过程,关于queue见:算法与数 ...

随机推荐

  1. tkinter + cefpython 仿美团桌面程序

    使用js开发桌面程序目前是一个趋势,Electron是其中一个佼佼者,网上也不乏很多文章.今天主要是来讲一下cefpython. 用python的朋友,特别使用过tkinter开发过界面的,一定会觉得 ...

  2. 接口测试中读取excel中的请求数据含有中文问题,UnicodeEncodeError: 'latin-1' codec can't encode character '\u5c0f' in position

    错误信息:UnicodeEncodeError: 'latin-1' codec can't encode character '\u5c0f' in position 31: Body ('小') ...

  3. AIX/Linux/HP-UX查看CPU/内存/磁盘/存储命令

    1.1    硬件环境验证方式 硬件环境主要包括CPU.内存.磁盘/存储.网络设备(如F5等).系统特有设备(如密押设备等)等,其中网络设备和系统特有设备由网络管理员或项目组提供为准,本节主要关注CP ...

  4. Hadoop 学习之路(二)—— 集群资源管理器 YARN

    一.hadoop yarn 简介 Apache YARN (Yet Another Resource Negotiator) 是hadoop 2.0 引入的集群资源管理系统.用户可以将各种服务框架部署 ...

  5. kafka 名词概念

    ProducerConsumerBrokerTopicPartitionConsumer Group分布式 Broker     Kafka集群包含一个或多个服务器,这种服务器被称为brokerTop ...

  6. C# Invoke

    最近在看一个项目,其中用到异步进程——控件Invoke,这里将具体用法介绍如下: 以进度条为例: //声明委托 delegate void SetProgressBarEnabledDelegate( ...

  7. 利用MAT分析JVM内存问题,从入门到精通(二)

    上一篇文章MAT入门到精通(一)介绍了MAT的使用场景和基本概念,这篇文章开始介绍MAT的基本功能,后面还有两篇,一篇是MAT的高级功能,另一篇是MAT实战案例分析. 三.欢迎页 使用MAT打开一个h ...

  8. Linux权限_用户_和用户组

    Linux中用户UID就判断操作系统中用户的身份. Centos7.x: 0:超级管理员 1-999:系统用户(包含Linux中自带服务) 1000以上   普通用户 Centos6.x : Root ...

  9. ReentrantLock源码的一点总结

    ReentrantLock 是可重入锁,可重入锁的意思就是同一个线程可以重复获得该锁. 如何做到可重复获得该锁?计数器实现. 第一次加锁,cas比较是不是0,是0设置为1,并设置当前拥有锁的线程: 第 ...

  10. 5. xadmin 后台搭建

    要维护他人产权,就不喽了,直接飞机 Django1.11.11使用xadmin的方法(一: 快速安装篇):https://www.jianshu.com/p/bcb74595213e Django1. ...