
Tree
iteye_17352
这个作者很懒,什么都没留下…
展开
-
Leetcode - Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j i...原创 2015-06-07 20:08:36 · 92 阅读 · 0 评论 -
Inorder Successor in BST
[分析] 参考[url]https://leetcode.com/discuss/59728/10-and-4-lines-o-h-java-c[/url] & [url]https://leetcode.com/discuss/59787/share-my-java-recursive-solution[/url] 不理解参考代码中为什么最后需要额外判断left.val > p.val ...原创 2015-09-26 17:49:27 · 227 阅读 · 0 评论 -
Lowest Common Ancestor of A Binary Tree
[分析] 最近公共祖先(LCA)是一个经典问题,以前没有好好研究过这个问题,不知道还有个Tarjan算法,今天开了眼界。一般有两种方法分别适用不同场景: 1)递归算法,适合在线单次查询,如本题; 2)Tarjan算法,适合批量查询,输入是一颗树和N对定点,为每个顶点(u,v)确定LCA。 有兴趣的同学看参考[url]https://github.com/julycoding/The-Ar...原创 2015-09-26 11:39:35 · 110 阅读 · 0 评论 -
Leetcode - Closest Binary Search Tree Value II
[分析] 思路1:遍历所有节点找到和 target最接近的 k 个元素,能否有个数据结构在遍历过程中维护已遍历元素离target最近的呢?PriorityQueue具备这种能力。我们需要个最小堆,堆元素需要保存两个信息,一个是树节点元素值,一个是这个元素和target的差的绝对值。但PriorityQueue是没有“堆底”概念的,当堆的size 增长到 k 后,如何删除堆中最大元素呢? 为实现删...原创 2015-09-13 14:16:30 · 107 阅读 · 0 评论 -
Leetcode - Graph Valid Tree
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree. For example: Given n = 5 ...原创 2015-09-02 08:50:37 · 309 阅读 · 0 评论 -
Leetcode - Closest Binary Search Tree Value II
Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target. Note: Given target value is a floating point. You may assume k is always valid, t...原创 2015-09-01 09:50:30 · 293 阅读 · 0 评论 -
Leetcode - Binary Tree Upside Down
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the origin...原创 2015-08-29 18:50:47 · 109 阅读 · 0 评论 -
Leetcode - Verify Preorder Sequence in Binary Search Tree
[分析] 思路1:暴力法,遍历当前待检查数组,找到第一个大于数组起始位置的位置i,则 i 为右子树根节点,然后递归判断左右子树。对于根节点,最坏情况下要遍历整个数组,时间为O(N),因为要递归检查每个节点,因此总的时间复杂度是O(N^2)。 思路2:参考StefanPochmann大神的作品[url]https://leetcode.com/discuss/51543/java-o-n-and...原创 2015-08-29 16:46:16 · 109 阅读 · 0 评论 -
Leetcode - Converted Sorted List to BST
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. [分析] 思路1复杂度是O(nlogn),实现过程中发现自己二指针法一开始是写错的,错误的结果是二分后左边会比右边多两个元素,在Reorder List中这种写法之所以也能...原创 2015-08-23 20:07:53 · 119 阅读 · 0 评论 -
Leetcode - Kth Smallest Element in BST
[分析] 思路1: 递归中序遍历,返回中序遍历中的第k个数。 思路2: 迭代方式实现的中序遍历,遍历到第k个数即返回。 思路3: 如果可以修改数结构,增加一个leftCount属性,记录当前节点左子树节点个数。 [ref] [url]http://bookshadow.com/weblog/2015/07/02/leetcode-kth-smallest-element-bst/[/...原创 2015-08-11 10:26:40 · 122 阅读 · 0 评论 -
Leetcode - Recover Binary Search Tree
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 devis...原创 2015-07-02 09:27:57 · 108 阅读 · 0 评论 -
Leetcode - 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's key. ...原创 2015-07-01 08:31:32 · 105 阅读 · 0 评论 -
Leetcode - Count Complete Tree Nodes
Definition of a complete binary tree from Wikipedia: In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible...原创 2015-06-07 20:51:00 · 108 阅读 · 0 评论 -
Leetcode - Unique Binary Search Trees
[题目] 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. [分析] n个节点的BST的结构数等于从1到n依次为root节点对应...原创 2015-04-10 10:00:59 · 107 阅读 · 0 评论