153-101. 对称二叉树

给定一个二叉树,检查它是否是镜像对称的。(随便看看吧,这个题真滴难-只有最后两个是对的前面的都不是对的)
# Definition for a binary tree node.
class TreeNode(object):
    def __init__(self, val=None, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution(object):
    def isSymmetric1(self, root):
        """这个方法适合满二叉树,获取是空节点补TreeNode()的树
        :type root: TreeNode
        :rtype: bool
        """
        temp = root
        stack = list()
        temp_list = []   # 几率元素个数
        while temp or stack:
            while temp:

                if not temp.right and temp.left:
                    temp.right = TreeNode(None)
                if temp.right and not temp.left:
                    temp.left = TreeNode(None)

                stack.append(temp)
                temp = temp.left

            cur = stack.pop()
            temp_list.append(cur.val)
            temp = cur.right

        return temp_list == temp_list[::-1]

    def isSymmetric2(self, root):
        """这个方法适合满二叉树,获取是空节点补TreeNode()的树
        :type root: TreeNode
        :rtype: bool
        """
        return self.preSearch(root) == self.postSearch(root)

    def preSearch1(self, root):
        temp = root
        stack = list()
        temp_list = []   # 几率元素个数
        while temp or stack:
            while temp:
                temp_list.append(temp.val)
                stack.append(temp)
                temp = temp.left
            cur = stack.pop()
            temp = cur.right
        return temp_list

    def postSearch1(self, root):
        temp = root
        stack = list()
        temp_list = []   # 几率元素个数
        while temp or stack:
            while temp:
                temp_list.append(temp.val)
                stack.append(temp)
                temp = temp.right
            cur = stack.pop()
            temp = cur.left
        return temp_list

    def add_node(self, root):
        if not root.right and root.left:
            root.right = TreeNode()

        if not root.left and root.right:
            root.left = TreeNode()

        if root.left:
            self.add_node(root.left)
        if root.right:
            self.add_node(root.right)

    def preSearch(self, root):
        print(root.val)
        if root.left:
            self.preSearch(root.left)

        if root.right:
            self.preSearch(root.right)

    def midSearch(self, root):
        if root.left:
            self.midSearch(root.left)

        print(root.val)

        if root.right:
            self.midSearch(root.right)

    def postSearch(self, root):
        if root.left:
            self.postSearch(root.left)

        if root.right:
            self.postSearch(root.right)

        print(root.val)

    def isSymmetric3(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        return self.check(root, root)

    def check(self, left, right):
        if not left and not right:
            return True
        if not left or not right:
            return False

        return left.val == left.val and self.check(left.left, right.right) and self.check(left.right, right.left)

    def isSymmetric(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        queue = list()
        queue.append(root)
        queue.append(root)
        while queue:
            left = queue.pop()
            right = queue.pop()

            if not left and not right:
                continue

            if (not left or not right) or (left.val != right.val):
                return False

            queue.append(left.left)
            queue.append(right.right)

            queue.append(left.right)
            queue.append(right.left)

        return True


if __name__ == '__main__':
    root = TreeNode(1)

    n1 = TreeNode(2)
    n2 = TreeNode(2)

    n3 = TreeNode(3)
    # n4 = TreeNode(4)

    n5 = TreeNode(3)
    # n6 = TreeNode(4)

    n1.left = n3
    # n1.right = n4

    n2.left = n5
    # n2.right = n6

    root.left = n1
    root.right = n2

    s1 = Solution()
    print(s1.isSymmetric(root))
posted @ 2021-01-22 14:55  楠海  阅读(53)  评论(0编辑  收藏  举报