代码随想录算法训练营第十四天 | 二叉树part02

82 阅读1分钟

代码随想录算法训练营第十四天 | 二叉树part02

226 翻转二叉树 (优先掌握递归)

image.png

递归法:前序遍历

if not root :
    return None
root.left , root.right = root.right , root.left
self.invertTree(root.left)
self.invertTree(root.right)
return root

迭代法:前序遍历

if not root :
    return None
stack = [root]
while stack:
    node = stack.pop()
    node.left,node.right = node.right,node.left
    if node.left:
        stack.append(node.left)
    if node.right:
        stack.append(node.right)
return root
101 对称二叉树 (优先掌握递归)

image.png

正是因为要遍历两棵树而且要比较内侧和外侧节点,所以准确的来说是一个树的遍历顺序是左右中,一个树的遍历顺序是右左中。

def isSymmetric(self, root: Optional[TreeNode]) -> bool:
    if not root:
        return True
​
    def isMirror(left: Optional[TreeNode], right: Optional[TreeNode]) -> bool:
        if not left and not right:
            return True
        if not left or not right:
            return False
​
        return (left.val == right.val) and \
               isMirror(left.left, right.right) and \
               isMirror(left.right, right.left)
​
    return isMirror(root.left, root.right)
104 二叉树的最大深度 (优先掌握递归)

递归法:

def maxdepth(self, root: treenode) -> int:
    return self.getdepth(root)
​
def getdepth(self, node):
    if not node:
        return 0
    leftheight = self.getdepth(node.left) #左
    rightheight = self.getdepth(node.right) #右
    height = 1 + max(leftheight, rightheight) #中
    return height

简化代码:

def maxdepth(self, root: treenode) -> int:
    if not root:
        return 0
    return 1 + max(self.maxdepth(root.left), self.maxdepth(root.right))
111 二叉树的最小深度 (优先掌握递归)

递归法版本一:

def getDepth(self, node):
    if node is None:
        return 0
    leftDepth = self.getDepth(node.left)  # 左
    rightDepth = self.getDepth(node.right)  # 右
​
    # 当一个左子树为空,右不为空,这时并不是最低点
    if node.left is None and node.right is not None:
        return 1 + rightDepth
​
    # 当一个右子树为空,左不为空,这时并不是最低点
    if node.left is not None and node.right is None:
        return 1 + leftDepth
​
    result = 1 + min(leftDepth, rightDepth)
    return result
​
def minDepth(self, root):
    return self.getDepth(root)

版本二:

class Solution:
    def minDepth(self, root):
        if root is None:
            return 0
        if root.left is None and root.right is not None:
            return 1 + self.minDepth(root.right)
        if root.left is not None and root.right is None:
            return 1 + self.minDepth(root.left)
        return 1 + min(self.minDepth(root.left), self.minDepth(root.right))

版本三:

class Solution:
    def __init__(self):
        self.result = float('inf')
​
    def getDepth(self, node, depth):
        if node is None:
            return
        if node.left is None and node.right is None:
            self.result = min(self.result, depth)
        if node.left:
            self.getDepth(node.left, depth + 1)
        if node.right:
            self.getDepth(node.right, depth + 1)
​
    def minDepth(self, root):
        if root is None:
            return 0
        self.getDepth(root, 1)
        return self.result