给一个排序数组(从小到大),将其转换为一棵高度最小的二叉搜索树。
class Solution:
"""
@param: A: an integer array
@return: A tree node
"""
def sortedArrayToBST(self, A):
# write your code here
if not A:
return
mid = len(A) // 2
node = TreeNode(A[mid])
node.left = self.sortedArrayToBST(A[:mid])
node.right = self.sortedArrayToBST(A[mid + 1:])
return node
给一棵非空二叉搜索树以及一个target值,找到在BST中最接近给定值的节点值
越写越绕,越写越多,最后都删了,重新写。
在考试的时候,如果写不出来很复杂的,就用最简单的办法去实现,不要管时间复杂度。
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
"""
class Solution:
"""
@param root: the given BST
@param target: the given target
@return: the value in the BST that is closest to the target
"""
def closestValue(self, root, target):
# write your code here
self.li = []
self.dic = {}
self.BST(root, target)
return self.dic[min(self.li)]
def BST(self, root, target):
if not root:
return
diff = abs(target - root.val)
self.li.append(diff)
self.dic[diff] = root.val
if target > root.val:
self.BST(root.right, target)
else:
self.BST(root.left, target)
def closestValue(self, root, target):
upper = root
lower = root
while root:
if target > root.val:
lower = root
root = root.right
elif target < root.val:
upper = root
root = root.left
else:
return root.val
if abs(upper.val - target) <= abs(lower.val - target):
return upper.val
return lower.val
给定一个确定根的二叉搜索树,返回树中任意两个不同节点的值的最小差。
写了一个,只能过部分测试。不太对。但是没过的那个测试,测试例子太多点了,没法调试。
class Solution:
"""
@param root: the root
@return: the minimum difference between the values of any two different nodes in the tree
"""
def minDiffInBST(self, root):
# Write your code here
self.min_val = float('inf')
self.dfs(root)
return self.min_val
def dfs(self, root):
if root.left is None and root.right is None:
return
if root.left:
left_diff = root.val - root.left.val
self.min_val = min(self.min_val, left_diff)
self.dfs(root.left)
if root.right:
right_diff = root.right.val - root.val
self.min_val = min(self.min_val, right_diff)
self.dfs(root.right)
下面是答案,答案就是去遍历,然后找最小值,我又犯了昨天的毛病,不肯用最暴力的方法求解。
import sys
class Solution(object):
def minDiffInBST(self, root):
"""
:type root: TreeNode
:rtype: int
"""
# 中序遍历,找到差的最小值。
nodes = []
queue= [root,]
while queue:
point = queue.pop(0)
nodes.append(point.val)
if point.left:
queue.append(point.left)
if point.right:
queue.append(point.right)
nodes.sort()
result = sys.maxint
for index,value in enumerate(nodes[1::]):
result = min(value - nodes[index],result)
return result
在編程中,maxint /INT_MAX表示可以由整數表示的最大值。在某些情況下,在編程時,我們可能需要分配一個大於任何其他整數值的值。通常,人們會手動分配這些值。例如,考慮一個必須使用for循環找出最小值的整數列表。
给定一个二叉排序树,按中序遍历重新排列树,使树中最左边的结点现在是树的根,并且每个结点没有左子结点,只有一个右子结点。
吸取了上一道题的教训,用了最简单的方法,不过看了答案,还是觉得自己想少了。
class Solution:
"""
@param root: a binary search tree
@return: Root of a tree
"""
def increasingBST(self, root):
# Write your code here.
li = []
self.dfs(root, li)
root = self.create_tree(li)
return root
def create_tree(self, li):
node = TreeNode(li[0])
root = node
for i in li[1:]:
node.right = TreeNode(i)
node = node.right
return root
def dfs(self, root, li):
if not root:
return
self.dfs(root.left, li)
li.append(root.val)
self.dfs(root.right, li)
class Solution:
def increasingBST(self, root):
ans = self.cur = TreeNode(0)
self.inorder(root)
return ans.right
def inorder(self, root):
if root:
self.inorder(root.left)
root.left = None
self.cur.right = root
self.cur = root
self.inorder(root.right)