
树
eeeeeeeeethan
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
7-4 Structure of a Binary Tree (30分)
7-4 Structure of a Binary Tree (30分) Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, a binary tree can be uniquely determined. Now given a sequence of statements about the stru原创 2020-07-21 21:34:48 · 228 阅读 · 0 评论 -
1099 Build A Binary Search Tree (30分)
先构造好树的结构,然后记录中序遍历下数组的下标。 将给出的值排序,按照中序遍历的下标顺序将各值填入树的结点中。 #include<iostream> #include<queue> #include<algorithm> #include<vector> using namespace std; struct node { int val, left, right; }table[100]; vector<int> inorder; voi..原创 2020-05-22 00:38:50 · 124 阅读 · 0 评论 -
1064 Complete Binary Search Tree (30分)
通过观察发现,对于既是BST又是CST的树,它的任意一个结点存在三种情况: 左子树是满二叉树,但右子树不是满二叉树 左子树不是满二叉树,但右子树是满二叉树 左右子树都是满二叉树 同时,由完全二叉树的定义可知:对于树的某个位置,它的左子树的结点数>右子树的结点数。我们可以从中序序列的中间开始向右逐个扫描,当遇到某个结点符合以上三种情况之一时就可以确定当前位置应该放置该结点。(中序序列通过对...原创 2020-04-14 17:41:51 · 157 阅读 · 0 评论 -
1021 Deepest Root (25分)
先用dfs数出图的连通分量数count,如果连通分量数count > 1直接输出结果; 如果count == 1,说明该图连通,又因为图中只有n - 1条边,该图必然为无环图,对每个根节点进行dfs,找出根的最大深度。 #include<cstdio> #include<unordered_set> #include<vector> #include&l...原创 2020-03-08 17:18:00 · 112 阅读 · 0 评论 -
1020 Tree Traversals (25分)
根据中序序列和后序序列的特性构造树。 #include<cstdio> #include<unordered_map> #include<queue> #include<vector> using namespace std; int n, postorder[30], inorder[30]; vector<int> res; uno...原创 2020-03-08 16:43:17 · 104 阅读 · 0 评论 -
1004 Counting Leaves (30分)
用一个结构体表示每个结点,借助队列对树进行层序遍历,记录没有子结点的结点。 #include<cstdio> #include<vector> #include<queue> using namespace std; int nodeNum, nonLeaf; struct node { int val; vector<int> childre...原创 2020-02-29 23:50:51 · 94 阅读 · 0 评论 -
1151 LCA in a Binary Tree (30分)
思路: 根据先序序列和中序序列建树 对于每对数字,分别记录它们的路径 找到最低公共结点 代码: #include<cstdio> #include<unordered_map> #include<vector> using namespace std; int pairNum, keyNum; int inorder[10001], preorder[100...原创 2020-02-09 13:13:42 · 135 阅读 · 0 评论 -
1143 Lowest Common Ancestor (30分)
思路一:根据先序序列依次将结点插入树中,之后对于给出的一对结点,分别找到两个结点从根到该结点的路径p1和p2。 #include<cstdio> #include<vector> using namespace std; int queryNum, keyNum; struct node { int val; node *left, *right; node(i...原创 2020-02-07 15:05:40 · 396 阅读 · 2 评论 -
1138 Postorder Traversal (25分)
由中序遍历的特点可知,对于序列中的某一个数字,在它左边的数字是它的左子树,在他右边的数字是它的右子树。我们根据先序序列的顺序对树进行访问,同时标记当前访问的数字在中序序列的位置。只要用一个表记录所有已经访问过的结点的位置,当某个数字左右两边都是访问过的数字,说明它没有子树了,就将这个数字输出。 #include<cstdio> #include<unordered_map>...原创 2020-02-06 14:34:49 · 198 阅读 · 0 评论 -
1135 Is It A Red-Black Tree (30分)
第一次尝试:没留意红黑树是BST,直接用回溯+非递归建树列举了所有可能情况: #include<cstdio> #include<stack> #include<vector> #include<algorithm> using namespace std; vector<int> preorder; struct node { i...原创 2020-02-05 16:15:39 · 183 阅读 · 0 评论 -
1130 Infix Expression
思路 表达式树非叶节点为操作符,叶节点为操作数,并且只要对其进行中序遍历就能得到表达式。 添加括号的时机:对于某个结点及其子树构成的子表达式,如果这个结点不是叶节点,就要在该子表达式两侧添加括号。 #include<string> #include<iostream> #include<unordered_set> using namespace std; ...原创 2020-02-04 23:13:33 · 112 阅读 · 0 评论 -
1127 ZigZagging on a Tree (30分)
注意用中序序列和后序序列构造二叉树的方法。 锯齿形遍历可以用双端队列实现。 #include<cstdio> #include<deque> #include<vector> using namespace std; int n; int inorder[50], postorder[50]; struct node { int val; node *le...原创 2020-02-03 10:51:10 · 115 阅读 · 0 评论 -
1123 Is It a Complete AVL Tree (30分)
注意事项: 插入后、旋转后要更新相应结点的高度 先更新子树高度、再更新结点高度 结点高度为该结点子树较高者高度+1 #include<cstdio> #include<cmath> #include<algorithm> #include<queue> #include<vector> using namespace std; i...原创 2020-02-02 10:56:33 · 204 阅读 · 0 评论 -
1119 Pre- and Post-order Traversals (30分)
对于后序序列,一个结点的访问总是先于这个结点的父结点的访问。可以利用这个结论,记录每个结点所有可能的父结点possibleParent,然后根据先序序列,配合使用栈,非递归地构造这棵树。 在建树过程中,对于每个结点,从栈中找出所有可能的父结点,并将结点插入到相应位置(左子树或右子树)。 通过递归,在构造过程中形成分支。如果某次构造不可能完整地构造出整棵树(没有合适的位置插入结点),则进行回溯。...原创 2020-02-01 13:53:28 · 355 阅读 · 0 评论 -
1043 Is It a Binary Search Tree (25分)
思路 题目给出一个先序序列,分析如下: 根据BST的性质,对于一个结点,它的左子树的所有结点的值小于该结点的值,它的右子树的所有结点的值小于等于该结点的值。 对于先序序列,第一个是根的值,之后是根的左子树的值的序列,最后是根的右子树的值的序列,如下图所示: 只要找出左右子树的分界,就可以递归地根据先序序列构造后序序列。可以分别从序列左侧和序列右侧开始向中间扫描,最后确定左右子树的分界线。如...原创 2020-01-10 13:11:04 · 198 阅读 · 0 评论 -
1066 Root of AVL Tree (25分)
#include<cstdio> #include<algorithm> using namespace std; struct node { int val, height; node *left, *right; node(int v) { val = v; height = 1; left = NULL; right = NULL; } }...原创 2020-01-14 19:55:47 · 132 阅读 · 0 评论