101. Symmetric Tree Leetcode Python,symmetricleetcode

來源:互聯網
上載者:User

101. Symmetric Tree Leetcode Python,symmetricleetcode

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 = Noneclass 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 = Noneclass 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        


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.