![](https://img-blog.csdnimg.cn/20201014180756780.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
各种二叉树
文章平均质量分 74
二叉树
master-dragon
weixin:dwl_1270530306
展开
-
1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30)题目地址A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:The left subtree of a node contains only nodes with keys原创 2015-08-13 15:15:34 · 547 阅读 · 0 评论 -
二叉树中两个结点的最近公共祖先(Lowest Common Ancestor of a Binary Tree)
http://ac.jobdu.com/problem.php?pid=1509 题目描述: 给定一棵树,同时给出树中的两个结点,求它们的最低公共祖先。 输入: 输入可能包含多个测试样例。 对于每个测试案例,输入的第一行为一个数n ,n<1000,代表测试样例的个数。 其中每个测试样例包括两行,第一行为一个二叉树的先序遍历序列,其中左右子树若为空则用0代替,其中二叉树的结点个数node_原创 2015-09-08 16:20:33 · 821 阅读 · 0 评论 -
各种二叉树的判断转化(leetcode 、pat)
keywords: bfs dfs recursion(递归)leetcode相关题目地址Symmetic(adj,相对称的,均衡的) Tree https://leetcode.com/problems/symmetric-tree/Same Tree https://leetcode.com/problems/same-tree/Validate Binary Search Tree原创 2016-09-25 12:05:44 · 804 阅读 · 0 评论 -
Unique Binary Search Trees (leetcode) 动态规划
有多少种不同的二叉搜索树?动态规划问题Unique Binary Search Trees题目地址 https://leetcode.com/problems/unique-binary-search-trees/可以以其中一个数为根节点,左右两边分开,如此递推ac代码class Solution {public: int numTrees(int n) { if (n原创 2016-09-27 19:56:29 · 436 阅读 · 0 评论 -
1102. Invert a Binary Tree (pat) / 226. Invert Binary Tree(leetcode)
二叉树的相关操作,用到并查集,递归,队列#include <cstdio>#include <cstdlib>#include <iostream>#include <vector>#include <queue>using namespace std;#define N 20int n ;struct data{ int left ; int right ;};data原创 2015-09-25 07:53:35 · 453 阅读 · 0 评论 -
222. Count Complete Tree Nodes(leetcode) 超时
题目地址 https://leetcode.com/problems/count-complete-tree-nodes/利用完全二叉树的性质,求一颗完整二叉树的节点总数采用遍历所有节点的方法会超时,需要利用递归求解 思路参考 西施豆腐渣 http://blog.csdn.net/xudli/article/details/46385011ac代码如下/** * Definition fo转载 2016-10-03 12:35:13 · 322 阅读 · 0 评论 -
二叉树的非递归遍历 (leetcode-重点掌握不使用栈的非递归)
原文链接:http://www.cnblogs.com/dolphin0520/archive/2011/08/25/2153720.html原创 2015-09-03 20:37:02 · 1792 阅读 · 0 评论 -
二叉树 层次遍历 (queue)遍历的变式题(leetcode)
利用队列结构和二叉树,变换求解116. Populating Next Right Pointers in Each Node题目地址 https://leetcode.com/problems/populating-next-right-pointers-in-each-node/117. Populating Next Right Pointers in Each Node II题目地址 h原创 2016-10-04 12:35:41 · 1127 阅读 · 0 评论 -
1020. Tree Traversals (25) - 已知后序和中序(前序和中序)求二叉树 (pat leetcode)
1020. Tree Traversals (25)题目地址Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order原创 2015-08-13 20:50:16 · 935 阅读 · 1 评论 -
1119. Pre- and Post-order Traversals (30)
题目地址https://www.patest.cn/contests/pat-a-practise/1119ac#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <iostream>#include <string>#include <vector>#include <queue>原创 2016-12-01 17:50:07 · 320 阅读 · 0 评论 -
折纸问题(牛客网) 。。。
问题和讨论 见牛客网 https://www.nowcoder.com/questionTerminal/430180b66a7547e1963b69b1d0efbd3c问题描述请把纸条竖着放在桌⼦上,然后从纸条的下边向上⽅对折,压出折痕后再展 开。此时有1条折痕,突起的⽅向指向纸条的背⾯,这条折痕叫做“下”折痕 ;突起的⽅向指向纸条正⾯的折痕叫做“上”折痕。如果每次都从下边向上⽅ 对折,对折N次转载 2016-12-14 14:31:43 · 2539 阅读 · 0 评论 -
1119. Pre- and Post-order Traversals (30) (先序+后序,确定二叉树?)
题目地址https://www.patest.cn/contests/pat-a-practise/1119题目描述Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder转载 2017-02-16 20:41:43 · 1211 阅读 · 0 评论 -
1110. Complete Binary Tree (25) -- 完全二叉树相关性质, 求树根两种方法
题目地址https://www.patest.cn/contests/pat-a-practise/1110题目描述Given a tree, you are supposed to tell if it is a complete binary tree.Input Specification:Each input file contains one test case. For each cas原创 2016-11-04 16:14:02 · 647 阅读 · 0 评论 -
二叉树的序列化和反序列化(C++) (leetcode)
考点: http://blog.csdn.net/weiwei22844/article/details/42004983参考其中的一个解题方法http://blog.csdn.net/brucehb/article/details/50472732C++实现(BFS 字符串处理)char* Serialize(TreeNode *root) { if (root == NULL)转载 2016-07-15 09:34:50 · 3313 阅读 · 0 评论 -
二叉树中两个节点的最近公共祖先(leetcode)
leetcode题目地址https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/#/description二叉树构造TreeNode* t1 = new TreeNode(3); TreeNode* t2 = new TreeNode(5); TreeNode* t3 = new TreeNode(1转载 2017-05-22 16:54:38 · 8527 阅读 · 0 评论 -
662. Maximum Width of Binary Tree(二叉树层次遍历,二叉树标序号)
层次遍历二叉树, queue<>每一层的结点数 在 whie(!que.empty()){}可以得到二叉树编号 i 的左孩子 2 * i i 的右孩子 2 * i + 1leetcode 题目地址,要注意题目描述ac代码/** * Definition for a binary tree node. * struct TreeNode { * int val; *原创 2017-09-22 20:32:37 · 375 阅读 · 0 评论 -
LeetCode(124) Binary Tree Maximum Path Sum 二叉树的最大路径和 (如何递归?)
转载 :http://blog.csdn.net/fly_yr/article/details/50412751注意递归思路转载 2016-11-01 21:40:44 · 347 阅读 · 0 评论 -
Unique Binary Search Trees 问题(leetcode )
理解二叉查找树96. Unique Binary Search Trees题目地址https://leetcode.com/problems/unique-binary-search-trees/题目描述Given n, how many structurally unique BST’s (binary search trees) that store values 1…n?For example原创 2016-10-25 20:40:37 · 223 阅读 · 0 评论 -
1053. Path of Equal Weight (30) -- dfs (leetcode 113. Path Sum II)
第二次做这个题,代码比第一次精简些,还是有待再进一步优化。第一次的 http://blog.csdn.net/qq_26437925/article/details/49329809思考过感觉收获更大思考1:为何是dfs 而不是 bfs 思考2; 结构体如何构造最好下面是ac代码:#include #include #include #include usin原创 2015-12-01 15:13:12 · 472 阅读 · 0 评论 -
Root of AVL Tree (25)
题目地址:http://www.patest.cn/contests/pat-a-practise/1066需要重点掌握 平衡二叉树的4种旋转(LL RR LR RL ) 学习网址: http://www.cnblogs.com/huangxincheng/archive/2012/07/22/2603956.html http://www.cnblogs.com/guyan/archive/原创 2015-08-22 14:04:51 · 490 阅读 · 0 评论 -
4 - 09. 笛卡尔树(25)
4 - 09. 笛卡尔树(25)题目地址4-09. 笛卡尔树(25)时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 笛卡尔树是一种特殊的二叉树,其结点包含两个关键字K1和K2。首先笛卡尔树是关于K1的二叉搜索树,即结点左子树的所有K1值都比该结点的K1值小,右子树则大。其次所有结点的K2关键字满足优先队列(不妨设为最小堆)的原创 2015-08-13 13:02:06 · 575 阅读 · 0 评论 -
1099. Build A Binary Search Tree (30)
题目地址:http://www.patest.cn/contests/pat-a-practise/1099搜索二叉树的建立与遍历,比较简单,采用合适的c++stl写很快#include <stdio.h>#include <stdlib.h>#include <iostream>#include <vector>#include <math.h>#include <string>#in原创 2015-08-18 14:51:56 · 414 阅读 · 0 评论 -
题目1467:二叉排序树
题目描述: 二叉排序树,也称为二叉查找树。可以是一颗空树,也可以是一颗具有如下特性的非空二叉树: 1. 若左子树非空,则左子树上所有节点关键字值均不大于根节点的关键字值; 2. 若右子树非空,则右子树上所有节点关键字值均不小于根节点的关键字值; 3. 左、右子树本身也是一颗二叉排序树。 现在给你N个关键字值各不相同的节点,要求你按顺序插入一个初始为空树的二原创 2015-09-06 10:35:39 · 928 阅读 · 0 评论 -
5-4 是否同一棵二叉搜索树 (25分)
5-4 是否同一棵二叉搜索树 (25分)给定一个插入序列就可以唯一确定一棵二叉搜索树。然而,一棵给定的二叉搜索树却可以由多种不同的插入序列得到。例如分别按照序列{2, 1, 3}和{2, 3, 1}插入初始为空的二叉搜索树,都得到一样的结果。于是对于输入的各种插入序列,你需要判断它们是否能生成一样的二叉搜索树。输入格式:输入包含若干组测试数据。每组数据的第1行给出两原创 2015-10-03 15:55:07 · 6079 阅读 · 0 评论 -
1043. Is It a Binary Search Tree (25) -- 二搜索叉树构建 和 递归遍历
1043. Is It a Binary Search Tree (25)A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:The left subtree of a node contains only nodes with k原创 2015-10-22 10:23:30 · 756 阅读 · 0 评论 -
5-5 Tree Traversals Again (25分)
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stac原创 2015-10-22 11:05:49 · 1117 阅读 · 0 评论 -
5-28 搜索树判断 (25分)
5-28 搜索树判断 (25分)对于二叉搜索树,我们规定任一结点的左子树仅包含严格小于该结点的键值,而其右子树包含大于或等于该结点的键值。如果我们交换每个节点的左子树和右子树,得到的树叫做镜像二叉搜索树。现在我们给出一个整数键值序列,请编写程序判断该序列是否为某棵二叉搜索树或某镜像二叉搜索树的前序遍历序列,如果是,则输出对应二叉树的后序遍历序列。输入格式:输入的第一行包原创 2015-10-29 10:32:53 · 2066 阅读 · 0 评论 -
5-7 Complete Binary Search Tree (30分)
5-7 Complete Binary Search Tree (30分)A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:The left subtree of a node contains only nodes with原创 2015-10-29 11:53:18 · 601 阅读 · 0 评论 -
5-4 List Leaves (25分)
5-4 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原创 2015-10-29 11:51:51 · 440 阅读 · 0 评论 -
5-31 笛卡尔树 (25分)
5-31 笛卡尔树 (25分)笛卡尔树是一种特殊的二叉树,其结点包含两个关键字K1和K2。首先笛卡尔树是关于K1的二叉搜索树,即结点左子树的所有K1值都比该结点的K1值小,右子树则大。其次所有结点的K2关键字满足优先队列(不妨设为最小堆)的顺序要求,即该结点的K2值比其子树中所有结点的K2值小。给定一棵二叉树,请判断该树是否笛卡尔树。输入格式:输入首先给出正整数N(原创 2015-10-31 07:53:29 · 2073 阅读 · 8 评论 -
5-26 Population (35分) -- 待解决
5-26 Population (35分)It is always exciting to see people settling in a new continent. As the head of the population management office, you are supposed to know, at any time, how people are d原创 2015-11-02 21:21:53 · 1036 阅读 · 0 评论 -
5-24 树种统计 (25分)
5-24 树种统计 (25分)随着卫星成像技术的应用,自然资源研究机构可以识别每一棵树的种类。请编写程序帮助研究人员统计每种树的数量,计算每种树占总数的百分比。输入格式:输入首先给出正整数N(≤105),随后N行,每行给出卫星观测到的一棵树的种类名称。种类名称由不超过30个英文字母和空格组成(大小写不区分)。输出格式:按字典序递增输出各种树的种类名原创 2015-11-02 08:03:43 · 2485 阅读 · 0 评论 -
1099. Build A Binary Search Tree (30)
题目地址:http://www.patest.cn/contests/pat-a-practise/1099/*1099. Build A Binary Search Tree (30)http://www.patest.cn/contests/pat-a-practise/1099*/#include <stdio.h>#include <stdlib.h>#include <math原创 2015-09-01 14:35:59 · 565 阅读 · 0 评论 -
满二叉树根据前序求后序
#include <cstdio> #include <iostream>using namespace std;#define N 100void pre2post(char pre[] , int preL,int preR , char post[] , int postL , int postR){ if(preL <= preR) { post[po原创 2015-09-04 19:41:22 · 828 阅读 · 0 评论 -
5-3 树的同构 (25分)
5-3 树的同构 (25分)给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树。而图2就不是同构的。图1图2现给定两棵树,请你判断它们是否是同构的。输入格式:原创 2015-10-10 10:38:48 · 3187 阅读 · 0 评论 -
834. Sum of Distances in Tree(leetcode, hard)
https://leetcode.com/problems/sum-of-distances-in-tree/例如:无向图如下 (参考: https://leetcode.com/problems/sum-of-distances-in-tree/discuss/130567/Two-traversals-O(N)-python-solution-with-Explanation)灰色 gr...原创 2019-06-23 22:51:16 · 287 阅读 · 0 评论