• leetcode-easy-trees-101. Symmetric Tree-YES


    mycode   92.44%

    # 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 deal(root,level):
                if not root:
                    self.res[level].append(None)
                    return 0
                self.res[level].append(root.val)  
                deal(root.left,level+1)         
                deal(root.right,level+1)
                
            from collections import defaultdict
            self.res = defaultdict(list) #传入int()函数来初始化
            deal(root,0)
            for level,item in self.res.items():
                if not item == item[::-1]:
                    return False
            return True
            
           
                

    参考

    # 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 traver(left,right):
                if not left and not right:
                    return True
                if not left or not right:
                    return False
                
                return (left.val == right.val) and traver(left.left,right.right) and traver(left.right,right.left)
            
            return traver(root,root)
                
  • 相关阅读:
    Java 抽象类
    Java 多态
    Java 重写与重载
    Java继承
    声卡驱动
    Sublime Text 3快捷键
    近年来世界各地ICO的花式骗局盘点
    区块链未能大爆发的影响因素有哪些?
    BCH分叉是一次站队博弈
    一文读懂百倍币的诞生背景
  • 原文地址:https://www.cnblogs.com/rosyYY/p/11022424.html
Copyright © 2020-2023  润新知