LeetCode's balanced binary tree (110), flipped binary tree (226)

1. Balanced Binary Tree (110)

Title description:

【simple】

Given a binary tree, judge whether it is a highly balanced binary tree.

In this question, a highly balanced binary tree is defined as:

The absolute value of the height difference between the left and right subtrees of each node of a binary tree does not exceed 1.

Example 1:

Insert picture description here

输入:root = [3,9,20,null,null,15,7]
输出:true

Topic link

Thinking analysis:

This is an extended version of finding the height of the tree , we only require whether the height difference between the left and right subtrees exceeds 1, and that's it!

Problem solution 1: top-down recursion

1. First, we need to define a height function to calculate the height of any node p in the binary tree
Insert picture description here

2. We can traverse the tree in preorder. For the nodes currently traversed, first calculate the height of the left and right subtrees. If the height difference between the left and right subtrees does not exceed 1, then recursively traverse the left and right child nodes and judge the left child. Whether the tree and the right subtree are balanced. This is a top-down recursive process.

# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isBalanced(self, root: TreeNode) -> bool:
        if not root:
            return True
        return abs(self.height(root.left)-self.height(root.right))<=1 and self.isBalanced(root.left) and self.isBalanced(root.right)
    def height(self,root:TreeNode)->int:
        if not root:
            return 0
        return max(self.height(root.left),self.height(root.right))+1
  • Time complexity: O (n 2) O(n^2)O ( n2)
  • Space complexity: O (n) O(n)O ( n )

Problem solution 2: bottom-up recursion

1. Since the first solution of the problem always repeatedly calculates the height of each node, we can judge whether the left and right subtrees of each node are balanced from the bottom up.

2. Bottom-up recursion is similar to post-order traversal. For the node currently traversed, first recursively judge whether the left and right subtrees are balanced, and then judge whether the subtree rooted at the current node is balanced. If a subtree is balanced, return its height (height must be a non-negative integer), otherwise return −1. If there is an unbalanced subtree, the entire binary tree must be unbalanced.

3. Compared with top-down recursion, this method only finds the height once for each node.

# 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 isBalanced(self, root: TreeNode) -> bool:
        def height(root: TreeNode) -> int:
            if not root:
                return 0
            leftHeight = height(root.left)
            rightHeight = height(root.right)
            if leftHeight == -1 or rightHeight == -1 or abs(leftHeight - rightHeight) > 1:
                return -1
            else:
                return max(leftHeight, rightHeight) + 1

        return height(root) >= 0
  • Time complexity: O (n) O(n)O ( n )
  • Space complexity: O (n) O(n)O ( n )

2. Flip the binary tree (226)

Title description:

[Simple]
Flip a binary tree.

Example:

enter:

     4
   /   \
  2     7
 / \   / \
1   3 6   9

Output:

     4
   /   \
  7     2
 / \   / \
9   6 3   1

Topic link

Thinking analysis:

1. To flip the binary tree, just flip the left and right subtrees recursively, and then swap the left and right subtrees.

# 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 invertTree(self, root: TreeNode) -> TreeNode:
        if not root:
            return root
        left=self.invertTree(root.left)
        right=self.invertTree(root.right)
        root.left,root.right=right,left
        return root
  • Time complexity: O (n) O(n)O ( n )
  • Space complexity: O (n) O(n)O ( n )

Guess you like

Origin blog.csdn.net/weixin_45666566/article/details/113241706