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 ...
分类:
其他好文 时间:
2019-08-20 01:06:24
阅读次数:
81
1.平衡二叉树基本介绍 1.1平衡二叉树又叫平衡二叉搜索树(Selg-balancing binary search tree),又叫AVL树,可以保证查询效率较高; 1.2 平衡二叉树是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是平衡二叉树。 1.3 平衡二叉树的常用实 ...
分类:
编程语言 时间:
2019-08-19 15:54:50
阅读次数:
99
Question 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: “ ...
分类:
其他好文 时间:
2019-08-19 09:34:44
阅读次数:
92
题目链接: "UVA 811" Description Once upon a time, in a faraway land, there lived a king. This king owned a small collection of rare and valuable trees, wh ...
分类:
其他好文 时间:
2019-08-19 00:07:04
阅读次数:
92
题目:验证一个二叉树是否为二叉排序树 注: 采用遍历二叉树的中序遍历,如果结果为排序,则说明该二叉树是二叉排序树 ...
分类:
其他好文 时间:
2019-08-18 17:49:52
阅读次数:
76
分析:比较容易想到的是用两个容器分别存放需要'1'变为'0'的值以及需要'0'变为'1'的值,1->0需要从大到小排序,0->1的需要从小到大排序进行贪心选择 但是如果存在这样的位置(变化前后都为1),我们就要枚举一开始把哪些这样的位置进行转换,显然是价值越大的越优先转换,因为这样代价小。 ...
分类:
其他好文 时间:
2019-08-18 12:10:11
阅读次数:
73
class BSTIterator { private Stack stack; public BSTIterator(TreeNode root) { stack = new Stack(); while(root != null){ stack.push(root); root = root..... ...
分类:
其他好文 时间:
2019-08-18 10:14:16
阅读次数:
90
Question 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 whic ...
分类:
其他好文 时间:
2019-08-18 09:59:24
阅读次数:
100
其实就是一个二分搜索 首先遍历LinkedList,记下链表长度 然后从mid开始, mid.left = 左部的mid mid.right = 右部的mid 依次递归,因为这相当于一个中序遍历,调用栈的最底层是从LinkedList首个元素开始的 ...
分类:
其他好文 时间:
2019-08-18 09:17:40
阅读次数:
74
二分法查找使用:二分法的输入为一个有序的元素列表,如果目标元素存在有序列表中,返回其位置,否则返回null; 使用二分法查找时,每次比较的元素均为有序列表的中间元素,每次都将余下的元素排查一半; 对于包含n个元素的有序列表,使用二分法查找最大复杂度为Olog(n)。 ...
分类:
其他好文 时间:
2019-08-17 10:56:52
阅读次数:
94