题目链接 https://loj.ac/problem/6669 题解 Orz yyf太神了,出这种又有意思又有意义的好题造福人类-- 首先\(n\)次询问求出所有节点的深度. 考虑按深度扩展(BFS), 同时维护重链剖分 每次扩展一个点时,从根节点所在重链开始,每次询问当前节点与链底节点的距离,这样就可以算出它们LCA的深度,也就是当前节点到根的路径上与这条重链相交部分的最大深度.那么如果这个最大深度等于当前深度\(-1\), 就得到了该点的父亲:否则跳到LCA的轻儿子所在重链上,继续询问即可…
题目: Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another co…
问题 给出一个二叉树,将其原地平面化为链表. 例如,给出: 1   /  \  2    5 / \     \ 3  4     6 平面化后的树看起来应该是这样: 1 \  2    \     3       \        4          \           5             \              6 初始思路 观察例子中平面化的过程,不难发现其实就是一个二叉树前序遍历的过程.让我们复习一下二叉树前序遍历的方法,根据wiki条目Tree traversal,…
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 和上一道题基本一样 根据中序和后序遍历确定一个棵树. /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeN…
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps until the tree is empty. Example:Given binary tree 1 / \ 2 3 / \ 4 5 Returns [4, 5, 3], [2], [1]. Explanation: 1. Remove the leaves [4, 5, 3] from the t…
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. For example, given preorder = [3,9,20,15,7] inorder = [9,3,15,20,7] Return the following binary tree: 3 / \ 9 20…
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. For example, given inorder = [9,3,15,20,7] postorder = [9,15,7,20,3] Return the following binary tree: 3 / \ 9 2…
Given preorder and inorder traversal of a tree, construct the binary tree. Note:  You may assume that duplicates do not exist in the tree. 利用前序和中序遍历构造二叉树的思想与利用中序和后续遍历的思想一样,不同的是,全树的根节点为preorder数组的第一个元素,具体分析过程参照利用中序和后续构造二叉树.这两道题是从二叉树的前序遍历.中序遍历.后续遍历这三种遍…
目录 题目描述: 示例: 解法: 题目描述: 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 示例: 给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 postorder = [9,15,7,20,3] 返回如下的二叉树: 3 / \ 9 20 / \ 15 7 解法: # define PR pair<int, int> /** * Definition for a binary tree node. * struct TreeNo…
题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)实现与根据先序和中序遍历构造二叉树相似,题目参考请进 算法思想 中序序列:C.B.E.D.F.A.H.G.J.I   后序序列:C.E.F.D.B.H.J.I.G.A   递归思路: 根据后序遍历的特点,…