C# leetcode 之 096 不同的二叉搜索树 题目描述 给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 二叉搜索树定义 左子树上所有节点的值小于根节点, 右子树上左右节点的值大于根节点, 并且左右子树也为二叉搜索树 解题思路 根据二叉搜索树定义可知, 针对 1 .. n 的排序序列, 选取其中任意一个元素i则1到i-1的元素为左子树节点, i+1到n为右子树的节点. 题目中问的是一共有多少种二叉树, 所以其实不用关心二叉树中节点的值. 由此可知一个树只要节点数相…
Leetcode:96. 不同的二叉搜索树 Leetcode:96. 不同的二叉搜索树 题目在链接中,点进去看看吧! 先介绍一个名词:卡特兰数 卡特兰数 卡特兰数Cn满足以下递推关系: \[ C_{n+1}=C_0C_n+C_1C_{n-1}+...+C_nC_0 \] 有兴趣的同学点击这里查看亚特兰数的百度百科 很巧的是,这道题可以利用亚特兰数计算出有多少个不同的BST. Don't talk,show me code! class Solution { public: int numTree…
今天是LeetCode专题第61篇文章,我们一起来看的是LeetCode95题,Unique Binary Search Trees II(不同的二叉搜索树II). 这道题的官方难度是Medium,点赞2298,反对160,通过率40.5%.我也仿照steam当中游戏评论的分级,给LeetCode中的题目也给出一个评级标准.按照这个点赞和反对的比例,这道题可以评到特别好评.从题目内容上来说,这是一道不可多得基础拷问的算法题,看着不简单,做起来也不简单,但看了题解之后,你会发现也没你想象得那么难.…
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another comput…
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) time and uses…
1. 题目 2. 解答 2.1. 方法一 在 LeetCode 108--将有序数组转化为二叉搜索树 中,我们已经实现了将有序数组转化为二叉搜索树.因此,这里,我们可以先遍历一遍链表,将节点的数据存入有序数组中,然后再将有序数组转化为二叉搜索树即可. class Solution { public: TreeNode* sortedListToBST(ListNode* head) { vector<int> nums; while (head != NULL) { nums.push_bac…
题目链接 : https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/ 题目描述: 给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定的有序链表: [-10, -3, 0, 5, 9], 一个可能的答案是:[0, -3, 9, -10, null, 5], 它可以表示下面这个高…
给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定的有序链表: [-10, -3, 0, 5, 9], 一个可能的答案是:[0, -3, 9, -10, null, 5], 它可以表示下面这个高度平衡二叉搜索树: 0 / \ -3 9 / / -10 5 采用二分的思路 将中间节点作为分割点,分成左子树和右子树. /** * Definition for singly-…
题目描述: 给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 示例: 输入: 3 输出: 5 解释: 给定 n = 3, 一共有 5 种不同结构的二叉搜索树: 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3 思路解析: 动态规划 假设n个节点存在 令G(n)表示从1到n可以形成二叉排序树个数 令f(i)为以i为根的二叉搜索树的个数 即有:G(n) = f(1) + f(2) + f(3) + f(4) + ... + f…
题目描述 给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定的有序链表: [-10, -3, 0, 5, 9], 一个可能的答案是:[0, -3, 9, -10, null, 5], 它可以表示下面这个高度平衡二叉搜索树: 0 / \ -3 9 / / -10 5 解题思路 转换二叉搜索树分为以下两步: 首先找到链表中间节点以及它的前一个节点,可以利用快慢指针的思想,接…
题目链接 [题解] 我们可以枚举这棵树的根节点在i处. 现在问题就变成. 1..i-1这i-1个节点组成的树和i+1..n这n-i个节点组成的树的个数的问题了. 假设他们俩的结果分别是cnt1和cnt2. 那么答案就是cnt1cnt2. 这显然是一个递归的问题. 因为4 5 6 7组成的二叉搜索树的个数肯定是和1 2 3 4相同的. 因此我们可以定义F(i)表示i个节点的搜索二叉树的个数. 则f[0] = f[1] = 1 f[i] = f[i-1]f[n-i] 时间复杂度O(N^2) 或者直接…
1. 题目 2.题目分析与思路 3.代码 1. 题目 给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数.节点的右子树只包含大于当前节点的数.所有左子树和右子树自身必须也是二叉搜索树. 2. 思路 这道题有多重解法,从根源来说,这是一个判断一棵树的中序遍历是不是一个升序. 2.1 使用递归 1.使用递归,使用递归的时候有一个坑,就是不能只判断当前值和左子树右子树的大小,而是要比较整棵树的极大值和极小值. 2.同时还要注意一点,…
109. 有序链表转换二叉搜索树 给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定的有序链表: [-10, -3, 0, 5, 9], 一个可能的答案是:[0, -3, 9, -10, null, 5], 它可以表示下面这个高度平衡二叉搜索树: 0 / \ -3 9 / / -10 5 class Solution { public TreeNode sortedLi…
[题目] 给出的升序排序的数组,个数必为奇数,要求形成二叉搜索(平衡)树. [思路] 辅助函数fun,[0,len]=>[0,mid-1]+[mid+1,len]. 当left>right,返回null. public TreeNode fun(int[] nums,int left,int right) { int mid=(right+left)/2; TreeNode tmp=new TreeNode(nums[mid]); if(right<left) return null;…
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Note: If the given node has no in-order successor in the tree, return null. 这道题让我们求二叉搜索树的某个节点的中序后继节点,那么我们根据BST的性质知道其中序遍历的结果是有序的, 是我最先用的方法是用迭代的中序遍历方法,然后用…
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree. You may assume each number in the sequence is unique. Follow up: Could you do it using only constant space complexity? 这道题让给了我们一个一维数组,让我们…
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has…
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 这道题是要求把有序链表转为二叉搜索树,和之前那道Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树思路完全一样,只不过是操作的数据类型有所差别,一个是数组,一个是链表.数组方便就方便在可以通过index直接访问任意一个元…
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道题是要将有序数组转为二叉搜索树,所谓二叉搜索树,是一种始终满足左<根<右的特性,如果将二叉搜索树按中序遍历的话,得到的就是一个有序数组了.那么反过来,我们可以得知,根节点应该是有序数组的中间点,从中间点分开为左右两个有序数组,在分别找出其中间点作为原中间点的左右两个子节点,这不就是是二分查找法的核…
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? confused what "{1,#,2,3}"…
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys…
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3 这道题实际上是Catalan Number卡塔兰数的一个例子,如果对卡塔兰数不熟悉的童鞋可能真…
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3 confused what "{1,#,2…
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example: Input: The root of a Binary Search Tree like thi…
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod…
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has…
Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary…
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target. Example 1: Input: 5 / \ 3 6 / \ \ 2 4 7 Target = 9 Output: True Example 2: Input: 5 / \ 3 6 / \ \ 2 4…
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example: Input: The root of a Binary Search Tree like thi…
Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 \ 3 / 2 Output: 1 Explanation: The minimum absolute difference is 1, which is the difference between 2 and 1…