Tree
二叉树
SYaoJun
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
437. 路径总和 III
题目路径总和 III代码考点:前缀和/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullp原创 2021-09-28 07:48:37 · 150 阅读 · 0 评论 -
高度最小的BST
题目类型:树难度:2题意:使用有序数组构建二叉查找树,标准模板题。注意树的定义在类之前有写,不需要自己重写。class MinimalBST {public: int buildMinimalBST(vector<int> vals) { TreeNode* root = build(vals, 0, vals.size()-1); ...原创 2020-04-11 09:49:37 · 217 阅读 · 0 评论 -
拓扑结构相同子树
题目子结构需要完全相同,可以使用二叉树序列化成字符串之后再比较。class IdenticalTree {public: bool dfs(TreeNode* A,TreeNode* B){ if(!A || !B) return A == B; return (A->val==B->val) && dfs(A->le...原创 2020-04-09 14:42:08 · 405 阅读 · 0 评论 -
94. 二叉树的中序遍历
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas...原创 2019-08-23 08:41:36 · 275 阅读 · 0 评论 -
面试题33:二叉搜索树的后序遍历序列
牛客网后序:左右根对于二叉排序树,左边一定比根小,右边一定比根大。class Solution {public: vector<int> seq; bool VerifySquenceOfBST(vector<int> sequence) { if(sequence.empty()) return false; se...原创 2019-08-15 08:52:21 · 147 阅读 · 0 评论 -
7-4 Cartesian Tree (30分)
A Cartesian tree is a binary tree constructed from a sequence of distinct numbers. The tree is heap-ordered, and an inorder traversal returns the original sequence. For example, given the sequence { 8...原创 2019-12-08 08:25:56 · 433 阅读 · 0 评论 -
1119 Pre- and Post-order Traversals (30分)
参考代码先存着吧,以后再慢慢理解,稍微加了一点注释。柳神代码利用前序和后序判断这颗二叉树是否唯一,输出任一中序遍历。主要是左右子树的划分。除了叶子节点外,只有一个孩子节点则会导致不唯一。#include <iostream>#include <vector>using namespace std;vector<int> in, pre, pos...原创 2019-12-05 22:24:40 · 161 阅读 · 0 评论 -
7-7 Complete Binary Search Tree (30 分)
BST+CBT+二叉树层序遍历按照从小到大的顺序插入开两个数组,第一个数组arr用来保存正序的数据, 第二数组brr用来记录BST插入时,在CBT中对应的下标。由于CBT左右子树满足二倍和二倍加1的关系,所以从小大输出时刚好满足层序遍历。当arr最小值插入时,对应着arr中第一个元素,而这个元素被存放在层序遍历数组的第r个下标上。#include <cstdio>#inc...原创 2019-11-25 17:41:31 · 949 阅读 · 0 评论 -
7-21 Counting Leaves (30 分)
树+DFS目标:统计每层的叶子节点个数主要是存储树的父子关系,然后统计。#include <cstdio>#include <iostream>#include <algorithm>#include <vector>#include <string.h>using namespace std;const int N =...原创 2019-11-24 22:59:37 · 351 阅读 · 0 评论 -
7-20 Binary Search Tree (25 分)
二叉搜索树把两棵树都建好再比较#include <cstdio>#include <iostream>#include <algorithm>using namespace std;struct tree{ int val; tree* left, *right; tree(int x){ val = x; left = right ...原创 2019-11-24 11:27:38 · 418 阅读 · 0 评论 -
846. 树的重心
注意ne数组和e数组的大小是顶点数的两倍每个顶点遍历一遍,搜索中计算和返回。#include <iostream>#include <cstring>#include <algorithm>using namespace std;const int N = 1e5+5, M = N*2;bool st[N]={false}; int h[N],...原创 2019-10-29 20:09:09 · 219 阅读 · 0 评论 -
7-31 笛卡尔树 (25 分)
二叉搜索树的判断方式和小根堆的判断方式#include <iostream>#include <vector>#include <string>#include <cmath>#include <algorithm>#include <queue>#include <cstdio>#include ...原创 2019-10-12 14:57:55 · 339 阅读 · 0 评论 -
7-4 是否同一棵二叉搜索树 (25 分)
题意:给出二叉排序树的插入序列,判断给出的这些序列跟第一个序列是否是同一棵树。重点:学会创建二叉搜索树学会判断两棵树是否是相同的两棵树#include <iostream>#include <vector>#include <string>#include <cmath>#include <algorithm>#inc...原创 2019-10-12 13:36:59 · 363 阅读 · 0 评论 -
7-29 修理牧场 (25 分)
哈夫曼树的经典题#include <iostream>#include <vector>#include <string>#include <cmath>#include <algorithm>#include <queue>#include <cstdio>#include <cctype&...原创 2019-10-11 20:53:26 · 365 阅读 · 0 评论 -
7-3 树的同构 (25 分)
主要知识点1.根据输入格式建树,由于数据比较少,可以不适用传统的指针建树,可以建立静态的树,以下标作为查找。2.比较两棵树是否是异构树,使用递归的方式。在leetcode上面有类似的题,是比较两棵树是否是相同的树,这里异构的树稍微扩展了一下。#include <iostream>#include <vector>#include <string>#i...原创 2019-10-11 20:40:00 · 394 阅读 · 0 评论 -
589. N-ary Tree Preorder Traversal
/*// Definition for a Node.class Node {public: int val; vector<Node*> children; Node() {} Node(int _val, vector<Node*> _children) { val = _val; childre...原创 2019-08-22 09:38:41 · 124 阅读 · 0 评论 -
965. Univalued Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas...原创 2019-08-22 09:22:36 · 81 阅读 · 0 评论 -
145. 二叉树的后序遍历
递归写法/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */...原创 2019-08-22 09:44:03 · 132 阅读 · 0 评论 -
173. Binary Search Tree Iterator
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas...原创 2019-08-29 09:00:55 · 131 阅读 · 0 评论 -
124. 二叉树中的最大路径和
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas...原创 2019-08-30 07:23:38 · 212 阅读 · 0 评论 -
543. Diameter of Binary Tree
使经过根节点的路径最长,则路径长度为左边最长,加上右边最长路径。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NUL...原创 2019-08-30 17:18:39 · 314 阅读 · 0 评论 -
102. Binary Tree Level Order Traversal
二叉树的层次遍历/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };...原创 2019-08-31 22:17:10 · 179 阅读 · 0 评论 -
105. Construct Binary Tree from Preorder and Inorder Traversal
二叉树的后序和中序还原二叉树/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {}...原创 2019-08-31 22:18:04 · 149 阅读 · 0 评论 -
101. Symmetric Tree
根节点相等左子树的右孩子 == 右子树的左孩子左子树的左孩子 == 右子树的右孩子/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x...原创 2019-08-31 22:19:25 · 175 阅读 · 0 评论 -
863. All Nodes Distance K in Binary Tree
//建立无向图,用深搜把所有长度为k的节点找到。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), righ...原创 2019-09-05 07:46:10 · 119 阅读 · 0 评论 -
450. Delete Node in a BST
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas...原创 2019-09-05 07:46:45 · 128 阅读 · 0 评论 -
235. Lowest Common Ancestor of a Binary Search Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas...原创 2019-09-06 09:15:57 · 234 阅读 · 0 评论 -
144. 二叉树的前序遍历
Morris前序遍历空间复杂度O(1)O(1)O(1)时间复杂度O(n)O(n)O(n)/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : va...原创 2019-09-06 10:22:15 · 140 阅读 · 0 评论 -
L2-012 关于堆的判断 (25 分)
边插入边建堆而不是都读入完成后再调整样例4的child是指直接孩子堆从1开始比较好调整#include <iostream>#include <vector>#include <algorithm>#include <string>#include <unordered_map>using namespace std;...原创 2019-09-06 21:19:42 · 330 阅读 · 0 评论 -
L3-010 是否完全二叉搜索树 (30 分)
BST的题我很喜欢注意插入是右边的小,左边的大。#include <iostream>#include <algorithm>#include <string>#include <deque>#include <cstring>#include <queue>#include <vector>#i...原创 2019-09-07 22:35:27 · 517 阅读 · 0 评论 -
L3-016 二叉搜索树的结构 (30 分)
我哭了,调了一两个小时我开始以为是树退化成单链时,数据会超过int,但是都不是这TM给的数据不在插入的值范围内,真的坑死人虽然数据满足某种结构,但是这个数压根都没插入进来#include <iostream>#include <algorithm>#include <string>#include <deque>#include &l...原创 2019-09-08 10:00:07 · 478 阅读 · 0 评论 -
二叉树平衡检查
/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }};*/class Balance {public: boo...原创 2019-09-19 09:05:47 · 135 阅读 · 0 评论 -
检查是否为BST
rand()函数产生伪随机数,可以生成0到rand_max之间的一个随机数 srand()提供种子,称为随机数生成器的初始化器 头文件stdlib.h 下面是根据种子生成的1-6之间的10个随机数#include#includeint main(){ unsigned int seed; while(scanf("%d",&seed) != EOF){ sra原创 2018-02-01 17:52:50 · 310 阅读 · 0 评论 -
最近公共祖先
class LCA {public: int getLCA(int a, int b) { map<int, bool> mp; while(a){ mp[a] = true; a = a/2; } while(b){ if(mp[b]== t...原创 2019-09-19 17:13:41 · 182 阅读 · 0 评论 -
P9 中序遍历树并判断是否为二叉搜索树 (15 分)
对给定的有N个节点(N>=0)的二叉树,给出中序遍历序列,并判断是否为二叉搜索树。题目保证二叉树不超过200个节点,节点数值在整型int范围内且各不相同。输入格式:第一行是一个非负整数N,表示有N个节点第二行是一个整数k,是树根的元素值接下来有N-1行,每行是一个新节点,格式为r d e 三个整数,r表示该节点的父节点元素值(保证父节点存在);d是方向,0表示该节点为父节点的左儿...原创 2019-09-23 21:32:27 · 1847 阅读 · 3 评论 -
1130 Infix Expressio (25 分)
之前因为偷懒没有刷这道题,结果秋季的PAT考试就考到了。主要是理解括号的插入位置以及遍历的规则。首先需要找到根节点,其次是非根节点和叶子点需要加括号。#include <iostream>#include <cmath>#include <algorithm>#include <vector>#include <string>...原创 2019-09-27 08:24:38 · 120 阅读 · 0 评论 -
7-3 Postfix Expressio (25 分)
考查的知识点:只有右子树的时候需要特殊处理#include <iostream>#include <cmath>#include <algorithm>#include <vector>#include <string>using namespace std;const int N = 25;int Root = 1;...原创 2019-09-27 09:23:54 · 214 阅读 · 0 评论 -
二叉树【北京大学】
牛客网题目链接求一颗完全二叉树两个节点的最近公共祖先题解先把一个节点到根节点的路径存起来,然后计算另一个节点到根节点的路径,没到一个祖先节点查一下另一个节点是否存在,若存在则一定是最近公共祖先。#include <iostream>#include <vector>#include <string>#include <cmath>#i...原创 2019-10-02 20:45:16 · 180 阅读 · 0 评论 -
7-4 Structure of a Binary Tree (30 分)
要判断给出的点是否出现过,不然即使在同一层或者是兄弟节点也是无效的。#include <iostream>#include <vector>#include <string>#include <cmath>#include <algorithm>#include <queue>#include <cstdi...原创 2019-10-04 10:24:03 · 294 阅读 · 3 评论 -
L2-004 这是二叉搜索树吗? (25 分)
#include<cstdio>#include<vector>#include<iostream>using namespace std;vector<int> A,post,pre,mir,postm;struct node{ int data; node* left, *right;};void insert(node* &...原创 2019-10-05 14:46:06 · 821 阅读 · 0 评论
分享