Leetcode Tags(8)Binary Search】的更多相关文章

一.475. Heaters 输入: [1,2,3],[2] 输出: 1 解释: 仅在位置2上有一个供暖器.如果我们将加热半径设为1,那么所有房屋就都能得到供暖. 输入: [1,2,3,4],[1,4] 输出: 1 解释: 在位置1, 4上有两个供暖器.我们需要将加热半径设为1,这样所有房屋就都能得到供暖. 1.思路:(1)先对heaters数组进行排序(2)对于每个house,计算其在heaters中的位置(3)计算这个house到其左和右heater的距离的最小值,也就是说heater只管离…
题目 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 us…
一.Easy 696 Count Binary Substrings Input: "00110011" Output: 6 Explanation: There are 6 substrings that have equal number of consecutive 1's and 0's: "0011", "01", "1100", "10", "0011", and "…
一.477.汉明距离总和 输入: , , 输出: 解释: 在二进制表示中,4表示为0100,14表示为1110,2表示为0010.(这样表示是为了体现后四位之间关系) HammingDistance(, ) + HammingDistance(, ) + HammingDistance(, ) = + + = . 1.常规做法,Time Limit Exceeded public int totalHammingDistance(int[] nums) { int sum = 0; int xo…
1.前序.中序.后序递归方式遍历二叉树 public void preOrderRecur(Node T) { if (T != null) { System.out.print(T.val + " "); preOrderRecur(T.left); preOrderRecur(T.right); } } public void inOrderRecur(Node T) { if (T != null) { inOrderRecur(T.left); System.out.print…
一.204. Count Primes Count the number of prime numbers less than a non-negative number, n. Input: 10 Output: 4 Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7. 1.数学原理:两个质数的乘积一定是合数.一个质数乘以任何数的积都一定不是质数.(除了1) 2.代码:需要注意的点:for (int…
一.500. Keyboard Row 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词. 输入: ["Hello", "Alaska", "Dad", "Peace"] 输出: ["Alaska", "Dad"] 可以重复使用键盘上同一字符. 可以假设输入的字符串将只包含字母. 思路:将键盘上每一行的字母映射到所在的行数,如果单词的所有字母中存在和第一个单词所在的行数不…
一.448. Find All Numbers Disappeared in an Array 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数字. 您能在不使用额外空间且时间复杂度为O(n)的情况下完成这个任务吗? 你可以假定返回的数组不算在额外空间内. 输入:[4,3,2,7,8,2,3,1] 输出:[5,6] 1.算法思路(本题关键点:注意数组下标的范围和…
1.知识点回顾 https://www.cnblogs.com/BigJunOba/p/9174206.html https://www.cnblogs.com/BigJunOba/p/9174217.html 2.典型例题(Easy) (1)707 Design Linked List Implement these functions in your linked list class: get(index) : Get the value of the index-th node in t…
一.232. Implement Queue using Stacks private Stack<Integer> stack; /** Initialize your data structure here. */ public e232() { stack = new Stack<>(); } /** Push element x to the back of queue. */ public void push(int x) { stack.push(x); } /** R…
int binary_search(int[] list, int item) { int low = 0; int high = list.length-1; while (low <= high) { int mid = int((low + high)/2); int guess = list[mid]; if (guess == item) { return mid; } if (guess > item) { high = mid -1; } else { low = mid+1;…
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, return its bottom-up level order traversal as: 分析 与…
LeetCode: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…
题目来源 https://leetcode.com/problems/validate-binary-search-tree/ 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'…
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…
题目来源: https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ 题意分析: 给定一个排好序的链表,将这个链表转换成一个高度平衡树. 题目思路: 有一个偷懒的方法,将链表转换成一个数组,然后用上一题的解法解决. 代码(python): # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x)…
题目来源: https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题意分析: 给出一个排好序的数组,根据这个数据形成一个高度平衡的搜索二叉树. 题目思路: 将中位数为根节点,中位数左边为左子树,右边为右子树. 代码(python): # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x)…
题目来源: https://leetcode.com/problems/recover-binary-search-tree/ 题意分析: 二叉搜索树中有两个点错了位置,恢复这棵树. 题目思路: 如果是没有空间复杂度限制还是比较简单.首先将树的值取出来,然后排序,将相应的位置判断是否正确.如果要特定空间复杂度就难很多. 代码(python): # Definition for a binary tree node. # class TreeNode(object): # def __init__…
题目来源: https://leetcode.com/problems/unique-binary-search-trees-ii/ 题意分析: 给一个整数,返回所有中序遍历是1到n的树. 题目思路: 这可以用递归的思想.首先确定根节点,如果k是根节点,那么1-k-1是左子树,而k+1-n为右子树. 代码(python): # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x):…
题目来源: https://leetcode.com/problems/unique-binary-search-trees/ 题意分析: 给定一个整数n,返回所有中序遍历是1到n的树的可能. 题目思路: 这是动态规划的题目.选定了第k个为根节点,那么所有的可能就是ans[k-1] * ans[n -k]其中,ans[i]代表i整数i一共有ans[i]种可能. 代码(python): class Solution(object): def numTrees(self, n): ""&q…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode.com/problems/trim-a-binary-search-tree/description/ 题目描述 Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree…
题目来源 https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Given a binary tree, flatten it to a linked list in-place. 题意分析 Input: binary tree Output: flattened tree Conditions:将一个二叉树压平为一个flatten 树,也就是一条斜线 题目思路 先将左右子树压平,然后将左子树嵌入到本节点与右子树之间.…
题目来源 https://leetcode.com/problems/minimum-depth-of-binary-tree/ Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 题意分析 Input: a binary tree…
题目来源 https://leetcode.com/problems/balanced-binary-tree/ Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never d…
题目来源 https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). 题意分析 Input: binary tree Output:…
题目来源 https://leetcode.com/problems/maximum-depth-of-binary-tree/ Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 题意分析 Input: tree Output:…
题目来源 https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). 题意分…
题目来源 https://leetcode.com/problems/binary-tree-level-order-traversal/ Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). 题意分析 Input: 一个二叉树 Output:一个每层数值组合的list Conditions:层次遍历 题目思路 采用…
题目来源 https://leetcode.com/problems/binary-tree-inorder-traversal/ iven a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,3,2]. 题意分析 Input:tree Output: inorder traversal Cond…
题目来源 https://leetcode.com/problems/search-in-rotated-sorted-array-ii/ Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given…