Leetcode| 104. 二叉树的最大深度、559. n叉树的最大深度、111.二叉树的最小深度、222. 完全二叉树的节点个数 Day16

104. Maximum Depth of Binary Tree

递归

本题可以使用前序(中左右),也可以使用后序遍历(左右中),使用前序求的就是深度,使用后序求的是高度。

  • 二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
  • 二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数后者节点数(取决于高度从0开始还是从1开始)

根节点的高度就是二叉树的最大深度

后序遍历求高度

# 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 maxDepth(self, root: Optional[TreeNode]) -> int:
        
        def getHeight(node):
            # 叶子节点的高度是1, 下一次None的高度是0
            if node == None:
                return 0
            leftHeight = getHeight(node.left)   # 左
            rightHeight = getHeight(node.right) # 右
            rootHeight = 1 + max(leftHeight, rightHeight)   # 根节点的高度
            return rootHeight

        return getHeight(root)

前序遍历求深度

# 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 __init__(self):
        self.res = 0

    def getDepth(self, node, depth):
        self.res = max(self.res, depth)       # 中
        if node.left == None and node.right == None:
            return
        if node.left:
            self.getDepth(node.left, depth + 1)  # 左
        if node.right:
            self.getDepth(node.right, depth + 1) # 右
        return

    def maxDepth(self, root: Optional[TreeNode]) -> int:
        if root == None:
            return 0
        self.getDepth(root, 1)
        return self.res

559. Maximum Depth of N-ary Tree

递归,后序遍历

"""
# Definition for a Node.
class Node:
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children
"""

class Solution:
    def maxDepth(self, root: 'Node') -> int:
        if not root:
            return 0
        depth = 0
        for i in range(len(root.children)):
            depth = max(depth, self.maxDepth(root.children[i]))

        return depth + 1    # 中节点
        

迭代,层序遍历

"""
# Definition for a Node.
class Node:
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children
"""
from queue import Queue
class Solution:
    def maxDepth(self, root: 'Node') -> int:
        que = Queue()
        res = 0
        if root == None:
            return 0
        que.put(root)
        while not que.empty():
            for _ in range(que.qsize()):
                node = que.get()
                if node.children:
                    for child in node.children:
                        que.put(child)
            res += 1

        return res

        

111. Minimum Depth of Binary Tree

递归,后序遍历

# 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
from queue import Queue
class Solution:
    def minDepth(self, root: Optional[TreeNode]) -> int:
        
        def getHeight(node):
            if node == None:
                return 0
            leftHeight = getHeight(node.left)   # 左
            rightHeight = getHeight(node.right) # 右

            # 当一个左子树为空,右不为空,这时并不是最低点
            if node.left == None and node.right != None:
                return 1 + rightHeight
            # 当一个右子树为空,左不为空,这时并不是最低点
            if node.left != None and node.right == None:
                return 1 + leftHeight
            rootMinHeight = 1 + min(leftHeight, rightHeight)
            return rootMinHeight

        return getHeight(root)

222. Count Complete Tree Nodes

递归,后序遍历

# 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 countNodes(self, root: Optional[TreeNode]) -> int:

        def getNodesNum(node):
            if node == None:
                return 0
            leftNum = getNodesNum(node.left)
            rightNum = getNodesNum(node.right)
            allNum = leftNum + rightNum + 1
            return allNum

        return getNodesNum(root)

迭代,层序遍历

# 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
from queue import Queue
class Solution:
    def countNodes(self, root: Optional[TreeNode]) -> int:
        res = 0
        que = Queue()
        if root == None:
            return res
        que.put(root)
        while not que.empty():
            node = que.get()
            res += 1
            if node.left:
                que.put(node.left)
            if node.right:
                que.put(node.right)

        return res
        

你可能感兴趣的:(算法,leetcode,python,算法)