
树
ZCAIHUI_
卷又卷不过,躺又躺不平
展开
-
二叉排序树
二叉排序树1.二叉排序树定义二叉排序树(BST),也称二叉查找树,亦称二叉搜索树。BST是一颗空树,或具有如下性质的非空二叉树: (1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值; (2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值; (3)左、右子树也分别为二叉排序树; 由此定义可知,BST是一个递归的数据结构,且BST中没有键值相等的节点。如果我们...原创 2018-09-11 18:21:53 · 509 阅读 · 0 评论 -
1102 Invert a Binary Tree (25 point(s))
The following is from Max Howell @twitter:Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a whiteboard so fuck off.Now it's your turn to...原创 2019-04-24 22:46:38 · 168 阅读 · 0 评论 -
1106 Lowest Price in Supply Chain (25 point(s))
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.Starting from one root supplier, everyone on th...原创 2019-04-25 00:51:12 · 180 阅读 · 0 评论 -
1053 Path of Equal Weight (30 point(s))
Given a non-empty tree with rootR, and with weightWiassigned to each tree nodeTi. Theweight of a path fromRtoLis defined to be the sum of the weights of all the nodes along the path ...原创 2019-07-13 01:29:14 · 163 阅读 · 0 评论 -
1135 Is It A Red-Black Tree (30 point(s))
There is a kind of balanced binary search tree namedred-black treein the data structure. It has the following 5 properties:(1) Every node is either red or black. (2) The root is black. (3) Every...原创 2019-08-04 21:17:08 · 175 阅读 · 0 评论 -
1130 Infix Expressions (25 point(s))
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with parentheses reflecting the precedences of the operators.Input Specification:Each input file contai...原创 2019-08-04 22:46:22 · 133 阅读 · 0 评论 -
1155 Heap Paths (30 point(s))
In computer science, aheapis a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (i...原创 2019-08-06 20:09:57 · 228 阅读 · 0 评论 -
L2-004 这是二叉搜索树吗? (25 point(s))
L2-004这是二叉搜索树吗?(25point(s))一棵二叉搜索树可被递归地定义为具有下列性质的二叉树:对于任一结点,其左子树中所有结点的键值小于该结点的键值; 其右子树中所有结点的键值大于等于该结点的键值; 其左右子树都是二叉搜索树。所谓二叉搜索树的“镜像”,即将所有结点的左右子树对换位置后所得到的树。给定一个整数键值序列,现请你编写程序,判断这是否是对一棵二叉搜索树或...原创 2019-08-13 20:17:15 · 203 阅读 · 0 评论 -
1123 Is It a Complete AVL Tree (30 point(s))
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is...原创 2019-08-09 10:29:51 · 162 阅读 · 0 评论 -
1110 Complete Binary Tree (25 point(s))
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 case, the first line gives a positive integerN(≤20) wh...原创 2019-08-09 12:56:15 · 197 阅读 · 0 评论 -
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 state...原创 2019-08-21 23:15:16 · 524 阅读 · 0 评论 -
1115 Counting Nodes in a BST (30 point(s))(创建BST,DFS统计每层结点个数)
1115Counting Nodes in a BST(30point(s))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 ...原创 2019-04-03 10:54:15 · 179 阅读 · 0 评论 -
1127 ZigZagging on a Tree (30 point(s)) (中序、后序建树and dfs深搜)
1127ZigZagging on a Tree(30point(s))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 and inorder trav...原创 2019-04-08 08:57:43 · 193 阅读 · 0 评论 -
层次遍历和后序遍构建二叉树
问题:给一棵二叉树的层序遍历序列和中序遍历序列,求这棵二叉树的先序遍历序列和后序遍历序列。分析:层次序列第一个元素肯定是根结点了,那么在读入后序元素过程中只需要确定它是根节点的左子树还是右子树问题了就可以解决问题了。因此核心点是:1)找根节点在中序遍历的下标 u 和 待插入结点val在中序遍历序列的下标k。2)判断u和v关系:u<k,则插入右子树;否则,插入左子树。核心代码:...原创 2019-04-07 23:48:53 · 1356 阅读 · 0 评论 -
1151 LCA in a Binary Tree(30 point(s))
1151 LCA in a Binary Tree原创 2018-09-09 14:12:24 · 1325 阅读 · 3 评论 -
平衡二叉树
平衡二叉树1.平衡二叉树的定义为了避免树的高度增长过快,降低二叉排序树的性能,我们规定在插入和删除节点时保证节点左右子树的高度差绝对值不超过一,这样的树称为平衡二叉树(也称AVL树),定义节点的左右子树的高度差为该节点的平衡因子,则此值只能为-1,0,1.特性:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。 引用2.作...原创 2018-09-12 21:40:27 · 635 阅读 · 0 评论 -
二叉树的非递归遍历和层次遍历
数据结构struct node{ int val; node *lchild,*rchild;};先序非递归遍历 1) 访问结点p,并将结点P入栈; 2) 判断结点P的左孩子是否为空,若为空,则取栈顶结点并进行出栈操作, 并将栈顶结点的右孩子置为当前的结点P,循环->1;若不为空,则将P的左孩子置为当前的结点P; 3) 直到P为NULL并且...原创 2018-11-02 21:40:21 · 519 阅读 · 1 评论 -
二叉树的最大深度与最小深度
最大深度1)非递归遍历 以层次遍历为例,对层次遍历稍加改写,遍历过程记录层数即可,遍历所有结点返回depth(注意与最小深度区分)/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; *...原创 2018-11-02 23:29:15 · 424 阅读 · 3 评论 -
完全二叉树的判断与交换左右子树
完全二叉树判断根据二叉树的定义,具有n个结点的完全二叉树与满二叉树中编号为1~n的结点一一对应。算法思想: 采用层次遍历的算法,将所有结点加入队列,(包括空结点) 当遇到空结点时,检查后面是否有非空结点。若有,则不是完全二叉树.bool IsComplete(node *root){ if(!root) return true; queue...原创 2018-11-15 22:38:27 · 554 阅读 · 0 评论 -
先序,中序,后序建树与最近祖先结点LCA
要唯一确定一颗二叉树,必须已知中序(先序,中序,后序之间)1:先序与中序建树(采用递归)思想:1)根据先序确定根节点位置。 2)根据根节点在中序序列的位置划分出二叉树的左右子树包含哪些结点 3)对左右子树递归上述步骤,直到数组为空。node *creat(int prel,int prer,int inl,int inr){ i...原创 2018-11-15 23:15:51 · 537 阅读 · 0 评论 -
二叉树的递归算法例题
二叉树递归算法:1. 统计二叉树中,度为0的结点个数2. 统计二叉树中,度为1的结点个数3. 统计二叉树中,度为2的结点个数4. 统计二叉树高度5. 统计二叉树宽度度6. 删除二叉树中所有叶子节点7. 交换每个节点的左右子女8. 判断一棵树是否为二叉排序树9. 找出给定结点在二叉树中的层次10. 判断二叉树是否为平衡二叉树首先,创建二叉树BiTre...原创 2019-03-25 23:05:42 · 2078 阅读 · 2 评论 -
1138 Postorder Traversal (25 point(s))(前序中序输出后序&不建树)
Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and inorder traversal sequences, you are supposed to output the first number of the postorder traversal se...原创 2019-04-04 07:30:43 · 220 阅读 · 0 评论 -
1094 The Largest Generation (25 point(s))
A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the largest population.Input ...原创 2019-04-10 22:51:01 · 159 阅读 · 0 评论 -
1099 Build A Binary Search Tree (30 point(s))
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 less than the node's key. The right su...原创 2019-04-02 09:38:01 · 224 阅读 · 0 评论 -
1119 Pre- and Post-order Traversals (30 分)
2月4号:update版本~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 and inorder traversal sequences, or preor...原创 2019-09-01 17:33:28 · 198 阅读 · 1 评论