![](https://img-blog.csdnimg.cn/20201014180756913.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
树
三块桌布
这个作者很懒,什么都没留下…
展开
-
lc 129.求根到叶子节点数字之和【***递归】
官方题解https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/solution/qiu-gen-dao-xie-zi-jie-dian-shu-zi-zhi-he-by-leetc//** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * ..原创 2020-10-29 19:42:49 · 105 阅读 · 0 评论 -
lc701.二叉搜索树插入
/** * 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(nullptr), right(nullptr) {}...原创 2020-09-30 19:12:44 · 83 阅读 · 0 评论 -
lc117.填充每个节点的下一个右侧节点指针II【①->lc102层序遍历,记录每层结点数;②*****在第n层为第n+1层建立next,在n+1层通过本层next为第n+2层建立next】
①->lc102层序遍历,记录每层结点数/*// Definition for a Node.class Node {public: int val; Node* left; Node* right; Node* next; Node() : val(0), left(NULL), right(NULL), next(NULL) {} Node(int _val) : val(_val), left(NULL), right(NULL..原创 2020-09-28 20:02:53 · 95 阅读 · 0 评论 -
lc235.二叉搜索树的最近公共祖先【①分别得到祖先序列,然后比较;②***同时查找,找出分岔结点】
①分别得到祖先序列,然后比较/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: TreeNode* l..原创 2020-09-27 17:29:15 · 517 阅读 · 0 评论 -
lc78.子集(子集:元素相同的排列只算一个)【①**枚举 - dfs回溯法(递归法)】【②***枚举 - 用掩码表示子集(迭代法)】
官方题解https://leetcode-cn.com/problems/subsets/solution/zi-ji-by-leetcode-solution/不是很懂【每种状态需要O(n)时间构造子集】。子集枚举 - 递归法(回溯法) :cur=0,1,2的时候都会递归两次,就是含nums[cur]往后递归一次,不含nums[cur]往后递归一次,所以一共有2^n个结果。递归时深度最多为n。class Solution {public: vector<...原创 2020-09-20 21:06:54 · 162 阅读 · 0 评论 -
lc113.路径总和II【***递归,DFS】->lc78
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: vector<vector<int&g...原创 2020-09-27 16:18:38 · 110 阅读 · 0 评论 -
已知后序中序,求先序
利用后序遍历的最后一个元素(根结点),在中序遍历中找到它,分割为左子树和右子树,然后在后序遍历中找到子树的根结点、起点、终点,分别递归。void pre(int rootPo,int start,int end){ if(start>end) return; int rootIn=start; //开始查找根结点在In[]中的位置 while((rootIn<end)&&(in[rootIn]!=post[rootPo])) { rootIn++.原创 2020-08-13 16:51:25 · 913 阅读 · 0 评论 -
lc106.从中序与后序遍历序列构造二叉树【①与“已知后序中序,求先序”一样的左右递归方法(分而治之);②*****迭代法,原理???】->lc105
①与“已知后序中序,求先序”一样的左右递归方法(分而治之)/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: ..原创 2020-09-25 20:12:36 · 128 阅读 · 0 评论 -
lc105.从前序与中序遍历序列构造二叉树【①与“已知后序中序,求先序”一样的左右递归方法(分而治之);②*****迭代法,原理???】
官方题解https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/solution/cong-qian-xu-yu-zhong-xu-bian-li-xu-lie-gou-zao-9/①与“已知后序中序,求先序”一样的左右递归方法(分而治之)②*****迭代法,原理???...原创 2020-09-25 19:52:06 · 202 阅读 · 0 评论 -
Tree Traversals Again 【中序+先序->后序】
7-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 stack operations are: push(1原创 2020-06-05 22:29:16 · 128 阅读 · 0 评论 -
二叉树前序遍历(非递归)
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: vector<int> preorderTr...原创 2020-08-10 17:00:03 · 122 阅读 · 0 评论 -
二叉树中序遍历(非递归)
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: vector<int> inorder...原创 2020-08-10 17:23:56 · 167 阅读 · 0 评论 -
lc145.二叉树后序遍历【*****非递归,用栈】
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: vector<int> postord...原创 2020-08-10 17:16:02 · 103 阅读 · 0 评论 -
lc.538 把二叉搜索树转换为累加树【反序中序遍历:①递归 ②*****线索二叉树Morris遍历】
官方题解https://leetcode-cn.com/problems/convert-bst-to-greater-tree/solution/ba-er-cha-sou-suo-shu-zhuan-huan-wei-lei-jia-sh-14/反序中序遍历 -递归/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * Tr...原创 2020-09-21 20:42:22 · 130 阅读 · 0 评论 -
lc501.二叉搜索树中的众数【线索二叉树Morris遍历->lc538】
开始想的按普通二叉树一样,把值和计数放在一个数组里面,但是想想它是二叉搜索树,有什么特别的呢?官方题解https://leetcode-cn.com/problems/find-mode-in-binary-search-tree/solution/er-cha-sou-suo-shu-zhong-de-zhong-shu-by-leetcode-/1)先中序遍历得到顺序数组,然后遍历数组并计数。2)*****Morris遍历(中序),使用跟lc538反序中序遍历一样的方法,一边建立线索,...原创 2020-09-24 19:40:52 · 142 阅读 · 0 评论 -
lc617.合并二叉树【用“指针=返回结点的函数”递归连接起二叉树】
官方题解https://leetcode-cn.com/problems/merge-two-binary-trees/solution/he-bing-er-cha-shu-by-leetcode-solution/node->left=mergeTrees(t1->left,t2->left); //用=函数返回结点 递归连接起树node->right=mergeTrees(t1->right,t2->right);这样得到的是一棵由原结点和新结...原创 2020-09-23 20:33:20 · 93 阅读 · 0 评论 -
lc968.监控二叉树【递归,*****状态转移&树形动态规划DP(dynamic programming)】
题解一https://leetcode-cn.com/problems/binary-tree-cameras/solution/shou-hua-tu-jie-cong-di-gui-you-hua-dao-dong-tai-g/核心:由左右孩子的abc三个值,得到node的abc,如此往下递归/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *l...原创 2020-09-22 23:45:22 · 212 阅读 · 0 评论 -
lc404.左叶子之和(二叉树遍历)
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int sumOfLeftLeaves(TreeNode...原创 2020-09-19 23:33:18 · 101 阅读 · 0 评论 -
lc684.冗余连接【并查集】
官方题解https://leetcode-cn.com/problems/redundant-connection/solution/rong-yu-lian-jie-by-leetcode/以及并查集的实现(浙大陈老慕课)https://blog.csdn.net/sankuaizhuobu/article/details/107979957class Solution {public: vector<int> findRedundantConnection(vec...原创 2020-09-18 19:20:07 · 184 阅读 · 0 评论 -
lc685.冗余连接II【和并查集有关,主要是分析附加边的几种情况】
官方题解https://leetcode-cn.com/problems/redundant-connection-ii/solution/rong-yu-lian-jie-ii-by-leetcode-solution/核心思路:class Solution {public: vector<int> findRedundantDirectedConnection(vector<vector<int>> edges){ ...原创 2020-09-18 19:29:30 · 179 阅读 · 0 评论 -
*****Huffman Codes【※构造哈夫曼树,※构造前缀树)】
【学到的东西】1)如何构造哈夫曼树(①优先队列、push*n、pop*2、push,同时累加,这样可以得到整棵树的带权路径长度(从一位大佬的代码里学到的)②直接构造二叉树(parent数组),可使用优先队列pop最小的两个出来(也可以用算法找出最小的两个数),合并后加入队列,看了另外一个大佬的代码,写的很好),以及如何由哈夫曼树得到哈夫曼编码(从叶子开始找parent,如果是parent的左孩子,则+0,右孩子+1,最后颠倒,也是在“另外一个大佬的代码”里看到的)2)如何构造字典树(next[],.原创 2020-08-15 16:58:21 · 170 阅读 · 0 评论 -
File Transfer【并查集:合并 & 查找】【※按秩归并,递归查找根结点】
题目7-8File Transfer(25分)We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it possible to send a file from any computer on the network to any other..原创 2020-08-13 16:10:46 · 196 阅读 · 0 评论 -
堆中的路径【堆:插入+到根结点的路径】
练习4.3堆中的路径(25分)将一系列给定数字插入一个初始为空的小顶堆H[]。随后对任意给定的下标i,打印从H[i]到根结点的路径。输入格式:每组测试第1行包含2个正整数N和M(≤1000),分别是插入元素的个数、以及需要打印的路径条数。下一行给出区间[-10000, 10000]内的N个要被插入一个初始为空的小顶堆的整数。最后一行给出M个下标。输出格式:对输入中给出的每个下标i,在一行中输出从H[i]到根结点的路径上的数据。数字间以1个空格分隔,行末不得有多余空格。输入样例:..原创 2020-08-12 21:22:05 · 150 阅读 · 0 评论 -
****lc102.二叉树层序遍历(分别得到每一层)(4个思路!)【推荐:使用DFS深度优先遍历,记录每个节点的层数】
思路:1、【双队列】【迭代(循环)】1号队列存储一层,2号队列存储下一层,1号出队至一个vector,等1号空了,2号也装入完毕,此时将2号复制到1号,2号自己清空,如此循环。 时间、空间复杂度O(n)2、【记录每层结点个数】【递归】每次出队的个数为每层的结点数,这些结点进入一个vector;下一层的一组结点进入下一个vector。3、【记录结点的分层】【迭代】层数改变(遇到分界结点),则装入下一个vector。不知道有什么问题,执行出错以上都是用的BFS广度优先遍历(使用队列),还...原创 2020-08-10 22:45:48 · 716 阅读 · 0 评论 -
List Leaves【建立树,层序遍历】
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 no.原创 2020-06-05 16:25:30 · 250 阅读 · 0 评论 -
二叉搜索树的操作集(※插入,※删除,查找)
6-12 二叉搜索树的操作集 (30分)本题要求实现给定二叉搜索树的5种常用操作。函数接口定义:BinTree Insert( BinTree BST, ElementType X );BinTree Delete( BinTree BST, ElementType X );Position Find( BinTree BST, ElementType X );Position FindMin( BinTree BST );Position FindMax( BinTree BST );其中原创 2020-08-02 16:37:19 · 120 阅读 · 0 评论 -
Complete Binary Search Tree
7-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 keys less than the node’s key.The right subtree of a node cont原创 2020-08-02 16:33:45 · 89 阅读 · 0 评论 -
Root of AVL Tree
7-6 Root of AVL Tree (25分)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 done to restore this propert原创 2020-08-02 16:31:43 · 154 阅读 · 0 评论 -
树的同构
基础实验4-2.1 树的同构 (25分)给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树。而图2就不是同构的。图1图2现给定两棵树,请你判断它们是否是同构的。输入格式:输入给出2棵二叉树树的信息。对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号);随后N行,第i行对应编号第i个结点,给出该结点原创 2020-06-05 16:31:01 · 179 阅读 · 0 评论