总结二叉树相关操作(python代码实现)
class TreeNode(object):
def __init__(self,x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def preOrder(self,root,res = []):
"""
:type root : TreeNode
:rtype : List[TreeNode.val]
"""
if not root:
return
res.append(root.val)
self.preOrder(root.left)
self.preOrder(root.right)
return res
def preOrder_2(self,root,res = []):
"""
:type root : TreeNode
:rtype : List[TreeNode.val]
"""
if not root:
return
stack = [root]
while stack:
s = stack.pop()
if s:
res.append(s.val)
stack.append(s.right)
stack.append(s.left)
return res
def inOrder(self,root,res = []):
"""
:type root : TreeNode
:rtype : List[TreeNode.val]
"""
if not root:
return
self.inOrder(root.left)
res.append(root.val)
self.inOrder(root.right)
return res
def inOrder_2(self,root,res = []):
"""
:type root : TreeNode
:rtype : List[TreeNode.val]
"""
if not root:
return
stack = []
while stack or root:
while root:
stack.append(root)
root = root.left
root = stack.pop()
res.append(root.val)
root = root.right
return res
def postOrder(self,root,res = []):
"""
:type root : TreeNode
:rtype : List[TreeNode.val]
"""
if not root:
return
self.postOrder(root.left)
self.postOrder(root.right)
res.append(root.val)
return res
def postOrder_2(self,root,res = []):
"""
:type root : TreeNode
:rtype : List[TreeNode.val]
"""
if not root:
return
stack = []
while stack or root:
while root:
stack.append(root)
if root.left:
root = root.left
else:
root = root.right
s = stack.pop()
res.append(s.val)
if stack and stack[-1].left == s:
root = stack[-1].right
else:
root = None
return res
def bfs(self,root,res = []):
"""
:type root : TreeNode
:rtype : List[TreeNode.val]
"""
if not root:
return
stack = []
stack.append(root)
while stack:
n = len(stack)
for i in range(n):
temp = stack.pop(0)
res.append(temp.val)
if temp.left:
stack.append(temp.left)
if temp.right:
stack.append(temp.right)
return res
def maxDepth(self,root):
"""
:type root : TreeNode
:rtype : int
"""
if not root:
return 0
return max(self.maxDepth(root.left),self.maxDepth(root.right))+1
def minDepth(self,root):
"""
:type root : TreeNode
:rtype : int
"""
if not root:
return 0
if not root.left and root.right:
return 1
if not root.left:
return 1 + self.minDepth(root.right)
if not root.right:
return 1 + self.minDepth(root.left)
return 1 + min(self.minDepth(root.right),self.minDepth(root.left))
def minDepth_2(self,root):
"""
:type root : treeNode
:rtype : int
"""
count = 1
if not root:
return 0
stack = []
stack.append(root)
while stack:
l = len(stack)
for i in range(l):
s = stack.pop(0)
if not s.left and s.right:
return count + 1
if s.left:
stack.append(s.left)
if s.right:
stack.append(s.right)
return count
def isMirror(self,root):
"""
:type root : TreeNode
:rtype : bool
"""
if not root:
return True
if not root.left and root.right:
return True
if not root.left or root.right:
return False
if root.left.val != root.right.val:
return False
return self.isMirror(root.left) and self.isMirror(root.right)
def isBalanced(self,root):
"""
:type root : treeNode
:rtype : bool
"""
def depth(root):
if not root:
return 0
return 1+max(depth(root.left),depth(root.right))
if not root:
return True
return abs(depth(root.left) - depth(root.right)) <=1 and self.isBalanced(root.left) and self.isBalanced(root.right)
def pathSum(self,root,sum):
"""
:type root : treeNode
:type sum : int
:rtype : int
"""
if not root:
return 0
def dfs(root,sum):
count = 0
if not root:
return 0
if sum == 0:
count += 1
count += dfs(root,left,sum-root.val)
count += dfs(root.right,sum-root.val)
return count
return dfs(root,sum) + self.pathSum(root.left,sum) + self.pathSum(root.right,sum)
def isCousins(self, root, x, y):
"""
:type root: TreeNode
:type x: int
:type y: int
:rtype: bool
"""
def depth(x):
if not x:
return 0
count = 1
stack = []
stack.append(root)
while stack:
n = len(stack)
for i in range(n):
s = stack.pop(0)
if s.val == x:
return count
if s.left:
stack.append(s.left)
if s.right:
stack.append(s.right)
count += 1
def father(x):
if not x:
return None
father = root
stack = []
stack.append(root)
while stack:
n = len(stack)
for i in range(n):
s = stack.pop(0)
if s.left.val == x or s.right.val == x:
father = s
return father
elif s.left:
stack.append(s.left)
elif s.right:
stack.append(s.right)
return depth(x) == depth(y) and father(x) != father(y)
if __name__ == '__main__':
A = TreeNode(1)
B = TreeNode(2)
C = TreeNode(3)
D = TreeNode(4)
E = TreeNode(5)
F = TreeNode(6)
A.left = B
A.right = C
B.left = D
B.right = E
C.right = F
print(Solution().preOrder(A))
print(Solution().preOrder_2(A))
print(Solution().inOrder(A))
print(Solution().inOrder_2(A))
print(Solution().postOrder(A))
print(Solution().postOrder_2(A))
print(Solution().bfs(A))
print(Solution().maxDepth(A))
print(Solution().minDepth(A))
print(Solution().minDepth_2(A))