530.二叉搜索树的最小绝对差
1. 在中序遍历的时候,用两个指针直接得出最小绝对差
2. 通过设置一个最大值的result,通过min函数不断去找到最小的
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def __init__(self):
self.pre = None # use to save prvious node
# we want to find min number, let result be a inf number at first
self.result = float('inf')
def getMinimumDifference(self, root):
"""
:type root: TreeNode
:rtype: int
"""
# recrusion stop condition
if(root== None):
return
# inorder traversal
# left
self.getMinimumDifference(root.left)
# inorder
if(self.pre != None):
# only when previous pointer has accese to node
# find mimmum
self.result = min(self.result,root.val-self.pre.val)
# since inorder after left, use left backtrack logic to move prvious pointer
self.pre = root
# right
self.getMinimumDifference(root.right)
return self.result
501.二叉搜索树中的众数
0. 二叉搜索树 一定使用中序遍历: 因为这样才能得到一个单调递增的搜索结果
1. maxCount 是所有最大出现频率
2. 当pre和cur相等的话
1. count 统计单个元素出现的频率
2. count + 1
3. count 默认 1
4. 如果当前元素和之前元素不相等,count重新回归到1
3. 如果count和maxCount相等的话
1. 把count添加到结果集数组里
4. 如果count > maxCount
1. 说明maxCount不是最大的了,需要更新整棵树最大的出现频率
2. 并删除之前数组新加入的值,并把当前的元素添加到数组(结果集)
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def __init__(self):
self.pre = None # define this pointer to store pervious node
self.result = [] # define a array to store final answer
self.count = 0 # record single element highest frequency
self.maxCount = 0 # record highest frequency in the BST
def searchBST(self,cur):
# recursion stop condition
if cur == None:
# since we iterate whole tree just return nothing
return
# inorder traversal
# left
self.searchBST(cur.left)
# inorder process
if(self.pre == None):
# it's first element, let count = 1
self.count = 1
elif(self.pre.val == cur.val):
# previous element == cureent element
self.count += 1
else:
# previous element is not equal to current element,reset the count
self.count = 1
# let pre pointer move
self.pre = cur
# if the single element frenqucy equal to BST highest frequency
if(self.count == self.maxCount):
# add our count to our result set
self.result.append(cur.val)
# update our maxCount
if(self.count > self.maxCount):
# since our single element frequency is larger than maxCount, we need update maxCount
self.maxCount = self.count
# clear our current array
self.result.clear()
# add our current largest element
self.result.append(cur.val)
# right
self.searchBST(cur.right)
# return answer
return
def findMode(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
self.pre = None # define this pointer to store pervious node
self.result = [] # define a array to store final answer
self.count = 0 # record single element highest frequency
self.maxCount = 0 # record highest frequency in the BST
self.searchBST(root)
return self.result
236. 二叉树的最近公共祖先
1. 使用后序遍历 从低往上 处理
2. 某一个节点左子树是否出现过p右子树是否出现过q
1. 将该节点向上返回
2. 这样就能把p跟q的公共祖先就传上来了
3. 如果遇到了p或者q就往上返回
4. 中的处理:
1. 判断左子树返回值不为空,说明出现了p或q
2. 判断右子树返回值不为空,说明出现p或q
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
# recursion end condition
if root == p or root == q or root == None:
# at those three condition we should return root
# since we should pass p or q or none to our parent node
return root
# single recursion logic
# left logic
left = self.lowestCommonAncestor(root.left,p,q)
# right logic
right = self.lowestCommonAncestor(root.right,p,q)
# inorder node process
if left is not None and right is not None:
# mean's it must be
return root
if left == None and right is not None:
# mean's right sub tree might have p or q, return right
return right
elif left is not None and right == None:
# same logic
return left
else:
# both left and right are None
return None