[LeetCode&Python] Problem 101. Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this 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:

    1
   /   2   2
   \      3    3

BFS and Iterative:

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

class Solution(object):
    def isSymmetric(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        que=[root]
        while que:
            check=[]
            n=len(que)
            for i in range(n):
                node=que.pop(0)
                if node:
                    que.append(node.left)
                    que.append(node.right)
                    check.append(node.val)
                else:
                    check.append(None)
            n=len(check)
            for i in range(n):
                if check[i]!=check[n-i-1]:
                    return False
        return True

  

Recursion:

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

class Solution(object):
    def isSymmetric(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        def findsys(node1,node2):
            if node1==None and node2==None:
                return True
            if node1==None or node2==None:
                return False
            return node1.val==node2.val and findsys(node1.left,node2.right) and findsys(node1.right,node2.left)

        return findsys(root,root)

  

原文地址:https://www.cnblogs.com/chiyeung/p/10200475.html

时间: 2024-11-08 00:00:36

[LeetCode&Python] Problem 101. Symmetric Tree的相关文章

<LeetCode OJ> 101. Symmetric Tree

101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Difficulty: Easy 给定一颗二叉树,检查是否镜像对称(环绕中心对称) Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this bin

[LeetCode&Python] Problem 590. N-ary Tree Postorder Traversal

Given an n-ary tree, return the postorder traversal of its nodes' values. For example, given a 3-ary tree: Return its postorder traversal as: [5,6,3,2,4,1]. Note: Recursive solution is trivial, could you do it iteratively? Recursion Solution: ""

[LeetCode&Python] Problem 589. N-ary Tree Preorder Traversal

Given an n-ary tree, return the preorder traversal of its nodes' values. For example, given a 3-ary tree: Return its preorder traversal as: [1,3,5,6,2,4]. Note: Recursive solution is trivial, could you do it iteratively? Recursion Method: ""&quo

LeetCode 101 Symmetric Tree (C)

题目: 101. Symmetric Tree QuestionEditorial Solution My Submissions Total Accepted: 135232 Total Submissions: 375037 Difficulty: Easy Contributors: Admin Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For

leetCode 101. Symmetric Tree 对称树

101. Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this 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]

【61】101. Symmetric Tree

101. Symmetric Tree Description Submission Solutions Add to List Total Accepted: 154374 Total Submissions: 414598 Difficulty: Easy Contributors: Admin Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For

LeetCode(1) Symmetric Tree

从简单的道题目開始刷题目: Symmetric Tree 题目:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 题目分析: 第一道题目简单的题目,

LeetCode之“树”:Symmetric Tree

Symmetric Tree 题目链接 题目要求: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 Note: Bonus points if y

101. Symmetric Tree Leetcode Python

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 Note: Bonus points if you could solve it both rec