![](https://img-blog.csdnimg.cn/20201014180756913.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
二叉树
涛涛酱
北邮研究僧在读
展开
-
【LeetCode 99】 Recover Binary Search Tree
题目描述二叉搜索树有两个节点的值被交换了,不改变树的结构,恢复二叉树。思路根据中序遍历顺序,找出不符合的节点。代码/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} *原创 2020-09-07 17:31:30 · 108 阅读 · 0 评论 -
【LeetCode 114】Flatten Binary Tree to Linked List
题目描述Given a binary tree, flatten it to a linked list in-place.For example, given the following tree: 1 / \ 2 5 / \ \3 4 6The flattened tree should look like:1 \ 2 \ 3 \ 4 \ 5 \原创 2020-07-13 13:10:50 · 152 阅读 · 0 评论 -
【LeetCode 95】 Unique Binary Search Trees II
题目描述Given an integer n, generate all structurally unique BST’s (binary search trees) that store values 1 … n.Example:Input: 3Output:[ [1,null,3,2], [3,2,null,1], [3,1,null,null,2], [2,1,3], [1,null,2,null,3]]Explanation:The above output原创 2020-06-04 21:48:12 · 129 阅读 · 0 评论 -
【LeetCode 530】 Minimum Absolute Difference in BST
题目描述Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.Example:Input: 1 \ 3 / 2Output:1Explanation:The minimum absolute difference is 1, which is the differ原创 2020-06-03 11:57:24 · 122 阅读 · 0 评论 -
【LeetCode 208】 Implement Trie (Prefix Tree)
题目描述Implement a trie with insert, search, and startsWith methods.Example:Trie trie = new Trie();trie.insert("apple");trie.search("apple"); // returns truetrie.search("app"); // returns falsetrie.startsWith("app"); // returns truetrie.insert(原创 2020-06-02 13:38:06 · 128 阅读 · 0 评论 -
【LeetCode 114】 Flatten Binary Tree to Linked List
题目描述Given a binary tree, flatten it to a linked list in-place.For example, given the following tree: 1 / \ 2 5 / \ \3 4 6The flattened tree should look like:1 \ 2 \ 3 \ 4 \ 5 \原创 2020-05-22 22:08:51 · 126 阅读 · 0 评论 -
【LeetCode 236】 Lowest Common Ancestor of a Binary Tree
题目描述Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q原创 2020-05-21 23:41:24 · 109 阅读 · 0 评论 -
【LeetCode 113】 Path Sum II
题目描述Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.Note: A leaf is a node with no children.Example:Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \原创 2020-05-21 20:34:04 · 107 阅读 · 0 评论 -
【LeetCode 687】Longest Univalue Path
题目描述Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root.The length of path between two nodes is represented by the number of edges between them.Example 1原创 2020-05-21 20:17:02 · 93 阅读 · 0 评论 -
【面试题 & LeetCode 814】Binary Tree Pruning
题目描述We are given the head node root of a binary tree, where additionally every node’s value is either a 0 or a 1.Return the same tree where every subtree (of the given tree) not containing a 1 has b...原创 2020-04-22 13:36:54 · 123 阅读 · 0 评论 -
【面试题 & LeetCode 124】Binary Tree Maximum Path Sum
题目描述Given a non-empty binary tree, find the maximum path sum.For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child conne...原创 2020-04-22 00:57:21 · 166 阅读 · 0 评论 -
【面试题 & LeetCode 112】Path Sum
题目描述Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.Note: A leaf is a node with no children.Examp...原创 2020-04-20 19:12:22 · 113 阅读 · 0 评论 -
【面试题 & LeetCode 199】Binary Tree Right Side View
题目描述Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.Example:Input: [1,2,3,null,5,null,4]Output: [1, 3,...原创 2020-04-17 12:56:24 · 109 阅读 · 0 评论 -
【二叉树】递归和非递归方式
先序遍历栈,先入根节点,循环当栈不为空,访问栈顶元素,右子树入栈,左子树入栈。中序遍历栈不为空或者当前节点不为空循环,当前节点不为空,当前节点入栈,到左节点。如果当前节点为空,访问栈顶元素,节点到右节点,继续循环。后序遍历两个栈。根节点入栈1,当栈不为空的时候,访问栈顶元素,入栈2,左子树入栈,右子树入栈。最后依次访问栈2的栈顶元素。https://zhuanlan.zhihu.com...原创 2020-04-12 12:28:16 · 379 阅读 · 0 评论 -
【剑指offer】二叉树问题总结
总结的挺好的,常拿出来看看。。。。https://juejin.im/post/5b8d64346fb9a01a1d4f99fa原创 2020-04-12 11:59:46 · 96 阅读 · 0 评论 -
【LeetCode 102】Binary Tree Level Order Traversal
题目描述层序遍历二叉树思路类似BFS,用两个值记录当前层节点个数和下一层节点个数。代码/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : v...原创 2020-04-12 00:10:05 · 66 阅读 · 0 评论 -
【LeetCode 894】 All Possible Full Binary Trees
题目描述A full binary tree is a binary tree where each node has exactly 0 or 2 children.Return a list of all possible full binary trees with N nodes. Each element of the answer is the root node of one ...原创 2020-03-03 15:00:28 · 104 阅读 · 0 评论 -
【LeetCode 543】Diameter of Binary Tree
题目描述Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or ...原创 2020-02-13 18:20:06 · 109 阅读 · 0 评论 -
【LeetCode 145】Binary Tree Postorder Traversal
题意Given a binary tree, return the postorder traversal of its nodes’ values.Example:Input: [1,null,2,3] 1 \ 2 / 3Output: [3,2,1]Follow up: Recursive solution is trivial, could...原创 2020-01-30 23:33:51 · 92 阅读 · 0 评论 -
【LeetCode 144】Binary Tree Preorder Traversal
题意Given a binary tree, return the preorder traversal of its nodes’ values.Example:Input: [1,null,2,3] 1 \ 2 / 3Output: [1,2,3]Follow up: Recursive solution is trivial, could ...原创 2020-01-30 18:27:27 · 63 阅读 · 0 评论 -
【LeetCode 94】Binary Tree Inorder Traversal
题目描述:Given a binary tree, return the inorder traversal of its nodes’ values.Example:Input: [1,null,2,3] 1 \ 2 / 3Output: [1,3,2]Follow up: Recursive solution is trivial, coul...原创 2020-01-27 15:04:39 · 73 阅读 · 0 评论 -
【LeetCode 295.】 Find Median from Data Stream
题目描述:实现数据结构,插入数据和查找中位数。思路:vector + sort, O (n2n^2n2)维护一个最大堆和一个最小堆,两者元素个数最大相差1,中位数即为最小堆堆顶元素或最大堆最小堆堆顶元素平均值。用优先队列实现。用二叉搜索树维护数组,两个指针指向中位数元素。用multi_set实现。代码:堆:class MedianFinder {public: /...原创 2019-07-17 15:45:12 · 88 阅读 · 0 评论 -
【LeetCode 218.】The Skyline Problem
题意:给出一组矩形的x1, x2, h参数,求出轮廓坐标。思路:扫描线算法。用堆或二叉搜索树维护当前有效的楼的高度,扫描楼的进入和离开端点,如果是进入,而且高度高于已有的楼高度,是一个端点。如果是离开端点,当前高度和第二高度比较,如果当前高度高,记录第二高端点。注意特殊情况,横坐标相等时按照什么顺序处理。heap的话需要手写移除函数。二叉搜索树可以用C++的multiset。代码:...原创 2019-07-17 19:51:21 · 109 阅读 · 0 评论 -
【LeetCode 108】Convert Sorted Array to Binary Search Tree
题目描述:给一个排序数组,输出任意一种构成的平衡BST结构,平衡即任意节点的左右子树深度差不大于1。思路:递归,当前数组的中间节点作为根节点,对节点两侧分别建树作为左右子树。代码:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...原创 2019-06-10 09:22:30 · 86 阅读 · 0 评论 -
【剑指 offer】二叉树的镜像
题目描述:操作给定的二叉树,将其变换为源二叉树的镜像。example:二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ 11 9 7 5...原创 2019-03-16 08:34:51 · 74 阅读 · 0 评论 -
【LeetCode 297.】 Serialize and Deserialize Binary Tree
题目描述:给二叉树编码和解码。思路:刷剑指offer的时候刷到过,用指针写的。很麻烦。用 C++ 的 stringstream来写简洁了很多。代码:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *r...原创 2019-07-17 10:37:35 · 123 阅读 · 0 评论 -
【LeetCode Contest 143.】1103 1104 1105 1106
这次没迟到,但是惨败。1103. Distribute Candies to People给n个人分糖果,每一圈比前一次增加n个,问每个人能分到多少个。前半个小时,一直试图推公式,最后发现while循环就过了。卒。1104. Path In Zigzag Labelled Binary Tree一棵二叉树,按照每层Zigzag的方式给节点标记,问从根节点到 label 节点的路径。试图...原创 2019-07-03 11:47:08 · 213 阅读 · 0 评论 -
【剑指 offer】序列化二叉树
题目描述:请实现两个函数,分别用来序列化和反序列化二叉树思路:二叉树的序列化是指:把一棵二叉树按照某种遍历方式的结果以某种格式保存为字符串,从而使得内存中建立起来的二叉树可以持久保存。序列化可以基于 先序、中序、后序、按层 的二叉树遍历方式来进行修改。原理都是一样的(即遍历顺序不同而已,对每个结点的处理都是一样的),序列化的结果是一个字符串,序列化时通过 某种符号表示空节点(#),以 ! ...原创 2019-04-22 20:39:28 · 91 阅读 · 0 评论 -
【剑指 offer】二叉树中和为某一值的路径
题目描述:输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的list中,数组长度大的数组靠前)思路:路径是从根节点到叶子结点的。dfs。对结果按照要求排序。代码:struct TreeNode { int val; struct TreeNode *left; st...原创 2019-04-17 19:31:42 · 72 阅读 · 0 评论 -
【剑指 offer】二叉搜索树的后序遍历序列
题目描述:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。思路:二叉搜索树(Binary Search Tree,BST)又称二叉排序树、二叉查找树。二叉排序树或者是一棵空树,或者是具有下列性质的二叉树:(1)若左子树不空,则左子树上所有结点的值均小于或等于它的根结点的值;(2)若右子树不空,则右...原创 2019-04-16 21:41:43 · 81 阅读 · 0 评论 -
【剑指 offer】从上往下打印二叉树
题目描述:从上往下打印出二叉树的每个节点,同层节点从左至右打印。思路:bfs。先序遍历二叉树,把节点加入到队列当中,出队时打印该节点的值。和剑指offer的[把二叉树打印成多行] 思路相同。还不用换行。。。(https://blog.csdn.net/iCode_girl/article/details/88372146)代码:struct TreeNode { int val; ...原创 2019-04-16 17:35:35 · 78 阅读 · 0 评论 -
【剑指 offer】平衡二叉树
题目描述:输入一棵二叉树,判断该二叉树是否是平衡二叉树。思路:是空树或者左右子树高度差不超过1,并且左右子树都是平衡二叉树。代码:class Solution {public: bool IsBalanced_Solution(TreeNode* pRoot) { if (pRoot == NULL) return true; ...原创 2019-04-07 10:52:57 · 77 阅读 · 0 评论 -
【LeetCode 572】Subtree of Another Tree
题目描述:输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)思路:递归。代码:struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }...原创 2019-03-15 22:59:14 · 927 阅读 · 0 评论 -
【剑指 offer】二叉树的下一个节点
题目描述:给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。思路:有如下几种情况:如果一个节点有右子树,那么它的下一个节点就是它的右子树中的最左子节点。如果没有右子树,且它是父节点的左子节点,那么它的下一个节点就是它的父节点。如果一个节点即没有右子树,并且它还是父节点的右子节点,这种情况比较复杂。我们...原创 2019-03-10 16:43:04 · 84 阅读 · 0 评论 -
【剑指 offer】对称的二叉树
题目描述:请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。思路:二叉树和它的镜像相同,判断其为对称的。思路1: 先序遍历,和先序遍历的对称(根、右、左)比较,考虑到一个节点无法区分,把第一层NULL节点加进去了。并不知道出了什么bug。暂且放这。struct TreeNode { int val; struct...原创 2019-03-10 10:45:26 · 79 阅读 · 0 评论 -
【剑指 offer】把二叉树打印成多行
题目描述:从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。思路:对二叉树广搜,打印当前节点的时候,先把其子节点加入到队尾。用两个变量分别表示当前层还未打印节点数量和下层节点数量控制每层一行输出。代码:class Solution {public: vector<vector<int> > Print(TreeNode* pRoot) {...原创 2019-03-09 21:58:35 · 71 阅读 · 0 评论 -
【剑指 offer】二叉搜索树的第K个节点
题目描述:给定一棵二叉搜索树,请找出其中的第k小的结点。例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4。思路:二叉搜索树的中序遍历是有序的,所以返回中序遍历的第k个节点即可。代码:递归版:struct TreeNode { int val; struct TreeNode *left; struct TreeNode *r...原创 2019-03-09 16:58:59 · 95 阅读 · 0 评论 -
【剑指 offer】二叉搜索树与双向链表
题目描述:输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。思路:中序遍历。版本1不知道为什么提交上去反馈死循环,样例也没有找到不对的。版本2.。。是真的精辟。。。版本1:struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right;...原创 2019-04-18 17:23:22 · 80 阅读 · 0 评论 -
【LeetCode 236】Lowest Common Ancestor of a Binary Tree
题目描述:给一棵二叉树的根节点,找出两个节点的最近公共祖先。思路:递归在左右子树查找,如果当前根节点为空或与其中任一节点相同,返回。如果两边都找到非空节点,LCA为左右子树的根节点,否则返回非空节点。代码:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNo...原创 2019-05-14 16:38:16 · 82 阅读 · 0 评论 -
【LeetCode Contest 144. 】1108. 1109. 1110. 1111
2道。1108. Defanging an IP Address签到题。string的insert函数。1109. Corporate Flight Bookings模板题。树状数组,区间更新,单点查询。但我居然连赤果果的模板题也…调了半个多小时。1110. Delete Nodes And Return Forest比赛的时候觉得好难,今天晚上又自己写出来了。就是一个dfs。...原创 2019-07-08 19:43:39 · 168 阅读 · 0 评论