class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:
def recur(preorder,inorder,root,left,right,dic):
if left>right:return #数组左边界大于右边界 说明重建完毕
node=TreeNode(preorder[root])#前序遍历的首元素为根节点
i=dic[preorder[root]]#获得该根节点在中序遍历的索引值
node.left=recur(preorder,inorder,root+1,left,i-1,dic) #根节点左子树的:根节点,在中序遍历的左边界,右边界
node.right=recur(preorder,inorder,i-left+root+1,i+1,right,dic)
#根节点右子树的:根节点,在中序遍历的左边界,右边界
#i-left 是中序遍历中左子树的长度,加上前序遍历根节点的坐标root,可以得到左子树+根节点在前序遍历的总长度,
#那么再加1就在前序遍历中右子树的第一个节点,即为**右子树的根节点**。
#右子树的长度为在中序遍历中根节点坐标i开始到 右边界,即为i+1,right”“”
return node
dic=self.sovle(inorder)
return recur(preorder,inorder,0,0,len(preorder)-1,dic)
def sovle(self,inorder): #建立一个中序遍历的节点索引词典
dic={}
for index in range(0,len(inorder)):
dic[inorder[index]]=index
return dic
最开始想到的只是前序遍历把所有节点放到数组里,然后用双层循环比较每两个元素的差的绝对值和当前最小值。所写代码如下。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
import sys
class Solution:
def __init__(self):
self.nodeVals=[]
def minDiffInBST(self, root: TreeNode) -> int:
def recur(root: TreeNode):
if not root:
return
self.nodeVals.append(root.val)
recur(root.left)
recur(root.right)
recur(root)
curMin=sys.maxsize
for index in range(0,len(self.nodeVals)):
for index2 in range(index+1,len(self.nodeVals)):
curMin=min(curMin,abs(self.nodeVals[index]-self.nodeVals[index2]))
return curMin
但这样忽略了一个重要条件,对象是二叉搜索树,如果我们中序遍历一颗二叉搜索树,我们可以得到一个升序数组,那么我们只需要一层for循环就可以比较所有元素间的差值。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
import sys
class Solution:
def __init__(self):
self.nodeVals=[]
def minDiffInBST(self, root: TreeNode) -> int:
def recur(root: TreeNode):
if not root:
return
recur(root.left)
self.nodeVals.append(root.val)
recur(root.right)
recur(root)
curMin=sys.maxsize
print(self.nodeVals)
for index in range(0,len(self.nodeVals)-1):
curMin=min(curMin,abs(self.nodeVals[index]-self.nodeVals[index+1]))
return curMin
但这样不是最优的,我们完全可以在中序遍历的过程中完成比较
答案来自利口
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def minDiffInBST(self, root: TreeNode) -> int:
def dfs(root) :
if not root : return None
dfs(root.left)
self.minval = min(self.minval,root.val-self.preval)
self.preval = root.val
dfs(root.right)
self.minval = float('inf')
self.preval = float('-inf')
dfs(root)
return self.minval
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isSubStructure(self, A: TreeNode, B: TreeNode) -> bool:
def recur(A,B):
if not B:
return True
if not A or A.val !=B.val:
return False
return recur(A.left,B.left) and recur(A.right,B.right)
def issubTree(A,B):
if not A or not B:
return False
else:
return recur(A,B) or issubTree(A.left,B) or issubTree(A.right,B)
return issubTree(A,B)