A Tree

PTA——03-树2 List Leaves(25 分)【java语言实现】

倖福魔咒の 提交于 2020-05-08 06:38:17
03-树2 List Leaves(25 分) 题目 Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is the total number of nodes in the tree – and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a “-” will be put at the position. Any pair of children are separated by a space. Output Specification: For

[LeetCode] 101. Symmetric Tree 对称树

故事扮演 提交于 2020-05-07 00:18:51
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 But the following is not: 1 / \ 2 2 \ \ 3 3 Note: Bonus points if you could solve it both recursively and iteratively. 给一个二叉树判断是否为对称树。 解法1: 非递归,按层遍历,每一层检查一下是否对称。 解法2: 递归, 其中左子树和右子树对称的条件:1)两个节点值相等,或者都为空。2)左节点的左子树和右节点的右子树对称,左节点的右子树和右节点的左子树对称 Java:Recursion class Solution { public boolean isSymmetric(TreeNode root) { if (root == null) return true; return isSymmetric(root.left, root.right); } public boolean isSymmetric(TreeNode l,

1135. Is It A Red-Black Tree (30)

心不动则不痛 提交于 2020-05-04 04:42:04
1135. Is It A Red-Black Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue There is a kind of balanced binary search tree named red-black tree in the data structure. It has the following 5 properties: (1) Every node is either red or black. (2) The root is black. (3) Every leaf (NULL) is black. (4) If a node is red, then both its children are black. (5) For each node, all simple paths from the node to descendant leaves contain the same number of black nodes. For example, the tree in Figure 1 is a red-black tree, while the ones in Figure 2 and 3 are not. Figure 1

PAT 1135 Is It A Red-Black Tree

烂漫一生 提交于 2020-05-04 04:41:49
There is a kind of balanced binary search tree named red-black tree in the data structure. It has the following 5 properties: (1) Every node is either red or black. (2) The root is black. (3) Every leaf (NULL) is black. (4) If a node is red, then both its children are black. (5) For each node, all simple paths from the node to descendant leaves contain the same number of black nodes. For example, the tree in Figure 1 is a red-black tree, while the ones in Figure 2 and 3 are not. Figure 1 Figure 2 Figure 3 For each given binary search tree, you are supposed to tell if it is a legal red-black

1135 Is It A Red-Black Tree (30 分)

梦想与她 提交于 2020-05-04 03:31:20
1135 Is It A Red-Black Tree (30 分) There is a kind of balanced binary search tree named red-black tree in the data structure. It has the following 5 properties: (1) Every node is either red or black. (2) The root is black. (3) Every leaf (NULL) is black. (4) If a node is red, then both its children are black. (5) For each node, all simple paths from the node to descendant leaves contain the same number of black nodes. For example, the tree in Figure 1 is a red-black tree, while the ones in Figure 2 and 3 are not. Figure 1 Figure 2 Figure 3 For each given binary search tree, you are supposed to

AtCoder Beginner Contest 133

Deadly 提交于 2020-04-28 21:31:16
AtCoder Beginner Contest 133 Time :7/7(Sun) 20:00 Website : AtCoder BC-133 C 题目描述: 给定区间[l,r],请你求出 f(i,j)=i*j%2019 (l<=i<j<=r) 的最小值。 数据范围: 0<=l,r<=$2*10^9$ 题解: 由于数据范围很大,一开始觉得暴力枚举是不可能的了,然后就找规律,以为和2019的什么性质有关。 后来突然发现,只要r-l>=2019,那么在这之中必定存在2019的倍数,答案就为0。反之,r-l<2019,直接暴力枚举就好了。haosaoa #include<bits/stdc++.h> using namespace std; typedef long long ll; ll l,r; int main(){ scanf("%lld %lld",&l,&r); if(r-l>=2019){puts("0");return 0;} ll ma=2019; for(ll i=l;i<=r;i++) for(ll j=i+1;j<=r;j++) if(i*j%2019<ma)ma=i*j%2019; printf("%lld",ma); } D 题目描述: 有n座山( n为奇数 ),编号1..n,构成一个环,每两座山之间有一个水坝,当下雨时,如果某座山收集到x的水(x为偶数

算法与数据结构基础

独自空忆成欢 提交于 2020-04-24 03:26:17
二叉树基础 满足这样性质的树称为二叉树:空树或节点最多有两个子树,称为左子树、右子树, 左右子树节点同样最多有两个子树。 二叉树是递归定义的,因而常用递归/DFS的思想处理二叉树相关问题,例如LeetCode题目 104. Maximum Depth of Binary Tree: // 104. Maximum Depth of Binary Tree int maxDepth(TreeNode* root) { if (root==NULL) return 0 ; return 1 +max(maxDepth(root->left),maxDepth(root-> right)); } 相关LeetCode题: 104. Maximum Depth of Binary Tree 题解 112. Path Sum 题解 437. Path Sum III 题解 100. Same Tree 题解 543. Diameter of Binary Tree 题解 563. Binary Tree Tilt 题解 257. Binary Tree Paths 题解 671. Second Minimum Node In a Binary Tree 题解 572. Subtree of Another Tree 题解 110. Balanced Binary Tree 题解 606.

Visual Studio 2019(VS2019)正式版注册码秘钥

时光毁灭记忆、已成空白 提交于 2020-04-20 14:25:43
Visual Studio 2019 Enterprise BF8Y8-GN2QH-T84XB-QVY3B-RC4DF Visual Studio 2019 Professional NYWVH-HT4XC-R2WYW-9Y3CM-X4V3Y 如果你看到这段文字,说明您正使用RSS阅读或转自《一棵树-博客园》,原文地址: https://www.cnblogs.com/atree/p/vs2019-professional-enterprise-key.html 来源: oschina 链接: https://my.oschina.net/u/4346514/blog/3276046

【图论】几个例题~

心不动则不痛 提交于 2020-04-15 08:54:54
【推荐阅读】微服务还能火多久?>>> 并查集最常用来判断图是否为连通图,或用来求图的连通分量数。 并查集1--<=>求连通分量个数 题目描述 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路? 输入描述: 测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1到N编号。 注意:两个城市之间可以有多条道路相通,也就是说 3 3 1 2 1 2 2 1 这种输入也是合法的 当N为0时,输入结束,该用例不被处理。 输出描述: 对每个测试用例,在1行里输出最少还需要建设的道路数目。 输入 4 2 1 3 4 3 3 3 1 2 1 3 2 3 5 2 1 2 3 5 999 0 0 输出 1 0 2 998 1 #include<bits/stdc++.h> 2 using namespace std; 3 int father[ 1005 ]; 4 int height[ 1005 ]; 5 void Initial( int n){//初始化 6

POJ1330 Nearest Common Ancestors【LCA】

微笑、不失礼 提交于 2020-04-13 16:53:53
【今日推荐】:为什么一到面试就懵逼!>>> Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 40900 Accepted: 20173 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each node is labeled with an integer from {1, 2,…,16}. Node 8 is the root of the tree. Node x is an ancestor of node y if node x is in the path between the root and node y. For example, node 4 is an ancestor of node 16. Node 10 is also an ancestor of node 16. As a matter of fact, nodes 8, 4, 10, and 16 are the ancestors of node 16.