101. Symmetric binary tree
Given a binary tree, check whether it is mirror symmetric.
For example, a binary tree [1,2,2,3,4,4,3] is symmetric.
1 / \ 2 2 / \ / \ 3 4 4 3
But the following [1,2,2, null, 3, null, 3] is not mirror symmetric:
1 / \ 2 2 \ \ 3 3
Can you use recursion and iteration to solve this problem?
Thinking of solving problems
How to judge whether a binary tree is mirror symmetric? As follows:
- The root node is null and returns true directly;
When the root node is not null, judge whether the left and right subtrees are symmetric
- Judgment root.left With root.right Is the node value the same
- Judgment root.left Is the left subtree of the root.right Right subtree symmetry of
- Judgment root.left Is the right subtree of the root.right Left subtree symmetry of
According to the [advanced] part of the title, this paper will solve this problem from two ideas of recursion and iteration.
According to the above idea of judging whether the binary tree is symmetric, first set the condition of recursive termination
- If both are null pointers, return true
- If one of them is not empty, return false
The specific process of recursion is as follows:
- First judge whether the current two pointer node values are equal
- Judge whether the left subtree of T1 and the right subtree of T2 are symmetrical
- Determine whether the right subtree of T1 and the left subtree of T1 are symmetrical
Please refer to【Code implementation: recursion】。
Or judge the symmetry of the binary tree according to the idea at the beginning, this time use the iterative method. A queue needs to be introduced here.
The specific methods are as follows:
- During initialization, the root node is queued twice.
- In the loop, two nodes are extracted twice each time they are out of the queue, and their values are compared (because the subtrees mirror each other, their values should be equal)
- When joining the team again, the left and right sub nodes of the two nodes are joined in the opposite order. (for example, the left child of node T1 and the right child of node T2 join the queue continuously)
- When the queue is empty, or when two consecutive outgoing nodes are not equal.
The specific implementation code is shown in【Code implementation: iteration】
Code implementation: recursion
# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def isSymmetric(self, root: TreeNode) -> bool: return self.check(root, root) def check(self, t1, t2): if t1 == None and t2 == None: return True if t1 == None or t2 == None: return False return t1.val == t2.val and self.check(t1.left, t2.right) and self.check(t1.right, t2.left)
Code implementation: iteration
# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def isSymmetric(self, root: TreeNode) -> bool: #Build, initialize queue #Queue the root node twice queue =  queue.append(root) queue.append(root) #Cycle starts while len(queue) != 0: #First out of the team, extract two nodes for comparison t1 = queue.pop() t2 = queue.pop() if t1 == None and t2 == None: continue #When two nodes are not equal, return false directly if t1 == None or t2 == None or t1.val != t2.val: return False #Join the team again, and join the left and right sub nodes of the two nodes in reverse order queue.append(t1.left) queue.append(t2.right) queue.append(t1.right) queue.append(t2.left) return True
Implementation result: recursion
Implementation result: iteration
- According to the meaning of the question, the problem is solved by recursion and iteration
The specific way to judge whether it is mirror symmetry is as follows
- Here, judge whether the root is not empty (if the root node is empty, return true directly);
- Judgment root.left And root.right Whether the node values are the same;
- Judgment root.left Left subtree sum of root.right Whether the right subtree of is symmetric or not;
- Judgment root.left Right subtree sum of root.right Whether the left subtree of is symmetric.
The process of recursion is as follows
- Termination condition: return true when all pointers are null; return false when one of the pointers is null;
- Judge whether the node values of two pointers are the same;
- Determine whether the left subtree of T1 is symmetric to the right subtree of T2
- Determine whether the right subtree of T1 is symmetric to the left subtree of T2
The specific process of iterative implementation is as follows
- Here, we need to introduce a queue. First, initialize the queue and queue root twice;
- At the beginning of the cycle, first queue twice, extract two nodes and compare their values (two adjacent values should be equal, because the left and right subtrees mirror each other)
- The left and right sub nodes of the two nodes are queued in reverse order (for example, the left node of T1 and the right node of T2 are queued continuously)
- When the queue is empty, or the two nodes out of the queue are not equal (that is, asymmetric).
Welcome to WeChat official account, book collection.