1. 题目:
给你一个二叉树的根节点 root , 检查它是否轴对称。
示例 1:
输入:root = [1,2,2,3,4,4,3]
输出:true
示例 2:
输入:root = [1,2,2,null,3,null,3]
输出:false
2. 我的代码:
# 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: Optional[TreeNode]) -> bool: # 结果 result = [] # 递归函数 def inorder(node): if node == None: return inorder(node.left) result.append(node.val) inorder(node.right) # 获取中序遍历结果 inorder(root) # 检查是否为回文串 left_p = 0 right_p = len(result) - 1 while left_p < right_p: if result[left_p] != result[right_p]: return False left_p += 1 right_p -= 1 return True
3. 错误情况: