题目: Given a root of Binary Search Tree with unique value for each node. Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You should keep the tree still a binary search tree after rem…
iven a root of Binary Search Tree with unique value for each node. Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You should keep the tree still a binary search tree after removal.…
从BST中移除一个节点是比较复杂的问题,需要分好几种情况讨论. 如这篇文章,就讨论了删除节点 1.有无左右子树 2.只有右子树 3.只有左子树 三种情况. 一种简单些的思维是只考虑删除节点是否有右子树(因为第一个比删除节点大的节点可能出现在右子树,不会出现在左子树). 这里用Target表示删除节点,Parent表示删除节点的母节点. 1. 没有右子树 Parent.left / Parent.right = Target.left 2. 有右子树 1) 找到第一个比删除节点大的节点Node 2…
Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.     可以采用类似于Covert Sorted Array to Binary Search Tree的方法,但是寻找中点对于链表来说效率较低 可以采用更高效的递归方式,无需寻找中点 注意引用传…
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 keys less than the node's key. The right subtree of a nod…
题目如下: Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original B…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.com/problems/find-mode-in-binary-search-tree/#/description 题目描述 Given a binary search tree (BST) with duplicates, find all the mode(s) (the most freque…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.com/problems/insert-into-a-binary-search-tree/description/ 题目描述 Given the root node of a binary search tree (BST) and a value to be inserted into the t…
Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST.   每次把中间元素当成根节点,递归即可   /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * Tre…
题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 思路: 找到数组的中间数据作为根节点,小于中间数据的数组来构造作为左子树,大于中间数据的数组来构造右子树. /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.l…
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 解题思路: 用一个有序数组,创建一个平衡二叉查找树. 为确保平衡,需要满足两子树的高度差不大于1,可以通过设置左子树结点数等于或者比右子树结点数多1,来实现. 那么每次取数组的中间位置后一个值,作为根结点,数组左边元素的插入左子树,数组右边元素插入右子树,依次类推. 代码: /** * Definiti…
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 有序数组变二叉平衡搜索树,不难,递归就行.每次先序建立根节点(取最中间的数),然后用子区间划分左右子树. 一次就AC了 注意:new 结构体的时候对于 struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x)…
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 思路:题目看上去好像很难,但实际上很简单,递归做就行,每次找到左右子树对应的子链表就行.一次AC. class Solution { public: TreeNode *sortedListToBST(ListNode *head) { if(head == NULL) retu…
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.思路:对于树来说,自顶向下的递归是很好控制的,自底向上的递归就很容易让脑神经打结了.本来想仿照排序二叉树的中序遍历,逆向地由数组构造树,后来发现这样做需要先确定树的结构,不然会一直递归下去,不知道左树何时停止.代码: TreeNode *addNode(vector<int> &num, int…
class Solution { public: vector<int> modes; int maxCnt = 0; int curCnt = 0; int curNum = 0; vector<int> findMode(TreeNode* root) { if (!root) { return modes; } curNum = root->val; inOrder(root); return modes; } void inOrder(TreeNode* root)…
Problem Link: http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Same idea to Convert Sorted Array to Binary Search Tree, but we use a recursive function to construct the binary search tree. # Definition for a binary tree 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…
The solution for the problem can be divided into three cases: case 1: if the delete node is leaf node, then we can simply remove it case 2: if the delete node is has single side or substree case 3: it has two children, then to keep it as a valid bina…
Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound,equal_range 本文是对Effective STL第45条的一个总结,阐述了各种查找算法的异同以及使用他们的时机. 首先可供查找的算法大致有count,find,binary_search,lower_bound,upper_bound,equal_range.带有判别式的如count_if,find_if或者binary_search的派别式版本,其…
题目描述: Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example: Given "b…
题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Example Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2->3.…
题目: Given a sorted linked list, delete all duplicates such that each element appear only once. Example Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3. 题解: Solution 1 () class Solution { public: ListNode *deleteDu…
Problem Link: http://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ We design a auxilar function that convert a linked list to a node with following properties: The node is the mid-node of the linked list. The node's left child i…
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题解:递归就可以了. Java代码如下: /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val =…
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 题解:开始想到的方法比较偷懒,直接遍历一遍数组,把每个ListNode对应的值放到数组里面,然后把数组转换成BST,想来这个题的本意肯定不是这样. 自己也想到了bottom-up的方法,但是没想明白怎么个bottom-up法,真的是昨天做梦都在想=.= 今天早上终于弄懂了,用递归…
题意: 输入一个正整数N(<=1000),接下来输入N个点的序号.如果刚才输入的序列是一颗二叉搜索树或它的镜像(中心翻转180°)的先序遍历,那么输出YES并输出它的后序遍历,否则输出NO. trick: for(auto it:post) cout<<it<<((it!=post[n-1])?" ":""); 这样输出会使第0,1数据点格式错误...原因未知 cout<<post[0]; for(int i=1;i<…
[题目] 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}…
/// Binary Search Tree - Implemenation in C++ /// Simple program to create a BST of integers and search an element in it #include<iostream> #include "cstdio" #include "queue" using namespace std; ///Definition of Node for Binary…
Given a binary search tree and a new tree node, insert the node into the tree. You should keep the tree still be a valid binary search tree. Notice You can assume there is no duplicate values in this tree + node.   Example Given binary search tree as…
题目: 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…