https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/Given a binary search tree (BST), find the lowest common ancestor (LCA) o...
分类:
其他好文 时间:
2015-07-11 16:18:22
阅读次数:
127
题意:给一棵二叉排序树,找p和q的LCA。思路:给的是排序树,那么每个节点必定,大于左子树中的最大,小于右子树种的最小。根据这个特性,找LCA就简单多了。三种情况:(1)p和q都在root左边,那么往root左子树递归。(2)在右同理。(3)一左一右的,那么root->val肯定大于其中的1个,小于...
分类:
其他好文 时间:
2015-07-11 15:05:18
阅读次数:
223
Well, remember to take advantage of the property of binary search trees, which is, root -> left -> val val right -> val. Moreover, both p and q will.....
分类:
其他好文 时间:
2015-07-11 15:02:07
阅读次数:
176
1、??Convert Sorted List to Binary Search TreeGiven a singly linked list where elements are sorted in ascending order, convert it to a height balanced ...
分类:
其他好文 时间:
2015-07-11 14:49:51
阅读次数:
100
题意:按层,将元素收集在一个二维数组中。思路:广搜应该是普遍的方法了。还能避免栈溢出,多好用。搭配deque,因为要经常删除。 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val;...
分类:
其他好文 时间:
2015-07-11 13:36:40
阅读次数:
96
题意:如题,平衡树是指任意一个节点(除了叶子),其左子树的高度与右子树的高度相差不超过1。思路:递归解决,但是提供的函数不满足递归的要求啊,我们至少得知道高度,又得返回真假,所以另开个函数解决。 1 /** 2 * Definition for a binary tree node. 3 * s.....
分类:
其他好文 时间:
2015-07-11 13:35:11
阅读次数:
92
题意:给一棵二叉树,求其深度。思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1。 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 ...
分类:
其他好文 时间:
2015-07-11 13:25:41
阅读次数:
125
题目:
Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
According to the
definition of ...
分类:
其他好文 时间:
2015-07-11 12:15:13
阅读次数:
169
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to thedefinition of LCA on Wikipedia: ...
分类:
其他好文 时间:
2015-07-11 10:27:33
阅读次数:
113