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. 深入浅出Ajax

    原文(我的GitHub):https://github.com/liangfengbo/frontend-ability/issues/1 学习大纲 理解Ajax的工作原理 Ajax核心-XMLHtt ...

  2. php.ini Xdebug配置

    在此记录: xdebug.profiler_output_dir="H:\install\phpStudy\tmp\xdebug"xdebug.trace_output_dir=& ...

  3. Netty源码分析--Reactor模型(二)

    这一节和我一起开始正式的去研究Netty源码.在研究之前,我想先介绍一下Reactor模型. 我先分享两篇文献,大家可以自行下载学习.  链接:https://pan.baidu.com/s/1Uty ...

  4. React躬行记(4)——生命周期

    组件的生命周期(Life Cycle)包含三个阶段:挂载(Mounting).更新(Updating)和卸载(Unmounting),在每个阶段都会有相应的回调方法(也叫钩子)可供选择,从而能更好的控 ...

  5. 一文详解 LVS、Nginx 及 HAProxy 工作原理( 附大图 )

    当前大多数的互联网系统都使用了服务器集群技术,集群是将相同服务部署在多台服务器上构成一个集群整体对外提供服务,这些集群可以是 Web 应用服务器集群,也可以是数据库服务器集群,还可以是分布式缓存服务器 ...

  6. sails连接monogodb数据库

    1.全局安装:cnpm install -g sails 2.命令窗口进入项目位置 新建项目:sails new sails_cqwu --fast,选择2(快速建立sails项目) 3.cd进入sa ...

  7. HBase 学习之路(五)——HBase常用 Shell 命令

    一.基本命令 打开Hbase Shell: # hbase shell 1.1 获取帮助 # 获取帮助 help # 获取命令的详细信息 help 'status' 1.2 查看服务器状态 statu ...

  8. zphp源码分析(一)

    zphp是一款轻量级的php服务端框架,是swoole官方开发的.可以用来开发web应用和网络服务. 安装: 可以通过composer安装, { "require": { &quo ...

  9. 👮 Golang Gin/Ace/Iris/Echo RBAC 鉴权库

    GRBAC 项目地址: https://github.com/storyicon/grbac Grbac是一个快速,优雅和简洁的RBAC框架.它支持增强的通配符并使用Radix树匹配HTTP请求.令人 ...

  10. selenium3+python3自动化测试学习之模拟事件处理

    自动化测试实战之ActionChains模拟用户行为 需要模拟鼠标操作才能进行的情况,比如单击.双击.点击鼠标右键.拖拽 解决:selenium提供了一个类来处理这类事件 selenium.webdr ...