目录
95. Unique Binary Search Trees II
96. Unique Binary Search Trees
102. Binary Tree Level Order Traversal
105. Construct Binary Tree from Preorder and Inorder Traversal
106. Construct Binary Tree from Inorder and Postorder Traversal
107. Binary Tree Level Order Traversal II
111. Minimum Depth of Binary Tree
116. Populating Next Right Pointers in Each Node
199. Binary Tree Right Side View
236. Lowest Common Ancestor of a Binary Tree
508. Most Frequent Subtree Sum
951. Flip Equivalent Binary Trees
1026. Maximum Difference Between Node and Ancestor
1339. Maximum Product of Splitted Binary Tree
95. Unique Binary Search Trees II
题目链接:https://leetcode.com/problems/unique-binary-search-trees-ii/
# 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 generateTrees(self, n):
"""
:type n: int
:rtype: List[TreeNode]
"""
vals=list(range(1,1+n))
if n==0:
return []
d={}
def construct(x):
if not x:
return [None]
elif len(x)==1:
return [TreeNode(x[0])]
elif d.__contains__(tuple(x)):
return d[tuple(x)]
res=[]
# print(x)
for i in range(len(x)):
left=x[:i]
right=x[i+1:]
left_node=construct(left)
right_node=construct(right)
for lef in left_node:
for rig in right_node:
y=TreeNode(x[i])
y.left=lef
y.right=rig
res.append(y)
d[tuple(x)]=res
return res
return construct(vals)
96. Unique Binary Search Trees
题目链接:https://leetcode.com/problems/unique-binary-search-trees/
class Solution(object):
def numTrees(self, n):
"""
:type n: int
:rtype: int
"""
vals=list(range(n))
d={}
def construct(x):
if not x:
return 0
elif len(x)==1:
return 1
elif d.__contains__(tuple(x)):
return d[tuple(x)]
res=[]
for i in range(len(x)):
left=x[:i]
right=x[i+1:]
if construct(left)*construct(right)==0:
res.append(construct(left)+construct(right))
else:
res.append(construct(left)*construct(right))
d[tuple(x)]=sum(res)
return sum(res)
return construct(vals)
100. Same Tree
题目链接:https://leetcode.com/problems/same-tree/
class Solution(object):
def isSameTree(self, p, q):
"""
:type p: TreeNode
:type q: TreeNode
:rtype: bool
"""
def pre(x,y):
if x==None and y==None:
return True
elif x==None or y==None:
return False
if x.val!=y.val:
return False
return pre(x.left,y.left) and pre(x.right,y.right)
return pre(p,q)
101.Symmetric Tree
题目链接:https://leetcode.com/problems/symmetric-tree/
class Solution(object):
def isSymmetric(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
path=[]
values=[]
if not root:
return True
if root.left==None and root.right==None:
return True
def r(node,p,v):
v.append(node.val)
if node.left==None and node.right==None:
path.append(p)
values.append(v)
return
if node.left:
k=p[::]
k.append(1)
r(node.left,k,v[::])
if node.right:
k=p[::]
k.append(-1)
r(node.right,k,v[::])
r(root,[],[])
print(path,values)
l=len(path)
if l!=len(values) or l%2==1:
return False
def list_equal(x,y):
if len(x)!=len(y):
return False
for i in range(len(x)):
if x[i]+y[i]!=0:
return False
return True
for i in range(l):
if list_equal(path[i],path[l-1-i]) and values[i]==values[l-i-1]:
pass
else:
return False
return True
102. Binary Tree Level Order Traversal
题目链接:https://leetcode.com/problems/binary-tree-level-order-traversal/
class Solution(object):
def levelOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
queue=[root]
res=[]
if not root:
return []
while queue:
# res.append()
tmp,k=[],[]
for i in queue:
k.append(i.val)
if i.left:
tmp.append(i.left)
if i.right:
tmp.append(i.right)
queue=tmp
res.append(k)
return res
105. Construct Binary Tree from Preorder and Inorder Traversal
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
# 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 buildTree(self, preorder, inorder):
"""
:type preorder: List[int]
:type inorder: List[int]
:rtype: TreeNode
"""
if not preorder:
return None
def construct(pre,ino):
if not pre:
return None
root=TreeNode(pre[0