LeetCode's symmetrical binary tree (101), the sum of left leaves (404), the longest path with the same value (687)

1. Symmetric Binary Tree (101)

Title description:

[Simple]
Given a binary tree, check whether it is mirror-symmetrical.

For example, the binary tree [1,2,2,3,4,4,3] is symmetric.

    1
   / \
  2   2
 / \ / \
3  4 4  3
 

Topic link

Thinking analysis:

1. Judging whether the mirror image is symmetrical. To be smaller, it is necessary to judge whether the left and right subtrees are symmetrical: their two root nodes have the same value and the right subtree of the left subtree is equal to the left subtree of the right subtree, and the left subtree is equal to the left subtree of the right subtree. The left subtree of the subtree is equal to the right subtree of the right subtree.
2. For obvious situations, you can use recursion to judge
3. Recursive termination conditions:

  • Two empty, return True

  • The two sides are asymmetric, there is a left side but no right side, and there is a right side but no left side, that is, one is empty, return False

  • Left subtree! = Right subtree, return False

# 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 isSymmetric(self, root: TreeNode) -> bool:
        if not root:
            return True
        def dfs(left,right):
			# 递归的终止条件是两个节点都为空
			# 或者两个节点中有一个为空
			# 或者两个节点的值不相等
            if not (left or right):
                return True
            elif not(left and right):
                return False
            elif left.val!=right.val:
                return False
            return dfs(left.left,right.right) and dfs(left.right,right.left)
        return dfs(root.left,root.right)
  • Time complexity: O (n) O(n)O ( n )
  • Space complexity: O (n) O(n)O ( n )

2. The sum of the left leaves (404)

Title description:

[Simple]
Calculate the sum of all left leaves of a given binary tree.

    3
   / \
  9  20
    /  \
   15   7

在这个二叉树中,有两个左叶子,分别是 915,所以返回 24

Topic link

Thinking analysis:

1. A node is a "left leaf" node, if and only if it is the left child node of a node, and it is a leaf node (no left or right children).
2. Traverse the entire tree. When we traverse to a node, if its left child node is a leaf node, then add the value of its left child node to the answer.

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def sumOfLeftLeaves(self, root: TreeNode) -> int:
        if not root:
            return 0
        sum_=0
        if root.left and root.left.left==None and root.left.right==None:
            sum_+=root.left.val
        return self.sumOfLeftLeaves(root.left)+self.sumOfLeftLeaves(root.right)+sum_

  • Time complexity: O (n) O(n)O ( n )
  • Space complexity: O (n) O(n)O ( n )

3. The longest path with the same value (687)

Title description:

[Medium]
Given a binary tree, find the longest path, each node in this path has the same value. This path may or may not pass through the root node.

Note: The length of the path between two nodes is represented by the number of edges between them.

Example 1:

enter:

              5
             / \
            4   5
           / \   \
          1   1   5

Output:

2

Topic link

Thinking analysis:

Guess you like

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