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 recursively and iteratively.

这道题目要求iterative 和recursive的解法。

recursive的解法比较简练

1.停止条件是 left==None & right==None

2. left.val==right.val 比较left.left right.right & left.right right.left

任何条件不成立都是false

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

class Solution:
    # @param root, a tree node
    # @return a boolean
    def sym(self,left,right):
        if left==None and right==None:
            return True
        if left and right and left.val==right.val:
            return self.sym(left.left,right.right) and self.sym(left.right,right.left)
        else:
            return False

    def isSymmetric(self, root):
        if root==None:
            return True
        return self.sym(root.left,root.right)

如果是iterative的解法需要用两个stack去存node

1.先将left 和right 依次存入stack1 stack2

2.当stack非空的时候将node pop出来 依次往下取left 和right

进行比较

代码如下

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

class Solution:
    # @param root, a tree node
    # @return a boolean
    def sym(self,left,right):
        if left==None and right==None:
            return True
        if left and right and left.val==right.val:
            return self.sym(left.left,right.right) and self.sym(left.right,right.left)
        else:
            return False

    def isSymmetric(self, root):
        if root==None:
            return True
        if root.left==None and root.right==None:
            return True
        if root.left==None or root.right==None:
            return False
        stack1=[]
        stack2=[]
        stack1.append(root.left)
        stack2.append(root.right)
        while len(stack1)!=0 and len(stack2)!=0:
            n1=stack1.pop()
            n2=stack2.pop()
            if n1.val!=n2.val:
                return False
            #if n1.left==None or n2.right==None:
             #   return False
            #if n1.right==None or n2.left==None:
             #   return False
            if n1.left==None and n2.right!=None or n1.left!=None and n2.right==None:
                return False
            if n1.right==None and n2.left!=None or n1.right!=None and n2.left==None:
                return False
            if n1.left and n2.right:
                stack1.append(n1.left)
                stack2.append(n2.right)
            if n1.right and n2.left:
                stack1.append(n1.right)
                stack2.append(n2.left)
        return True
        
时间: 2024-07-30 02:29:37

101. Symmetric Tree Leetcode Python的相关文章

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]

<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

Symmetric Tree leetcode java

题目: 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

【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&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 Ite

LeetCode 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 Note:Bonus

Leetcode 101 Symmetric Tree 二叉树

判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert Binary Tree,然后用Same Tree比较左右子树 而我的做法是改下Same Tree的函数,改动的是第27行 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNo

【LeetCode】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 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