`

Same Tree——Depth-first Search

 
阅读更多

Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

# 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):
    L = []
    R = []
    def getLChilds(self, root):
        if not root:
            self.L.append('#')
            return

        self.L.append(root.val)
        self.getLChilds(root.right)
        self.getLChilds(root.left)
    
    def getRChilds(self, root):
        if not root:
            self.R.append('#')
            return

        self.R.append(root.val)
        self.getRChilds(root.right)
        self.getRChilds(root.left)
        
    def isSameTree(self, p, q):
        """
        :type p: TreeNode
        :type q: TreeNode
        :rtype: bool
        """
        self.L = []
        self.R = []
        if not p and not q: return True
        if not p and q: return False
        if p and not q: return False
        self.getLChilds(p)
        self.getRChilds(q)
        if self.L == self.R: 
            return True
        else:
            return False
        

 

分享到:
评论

相关推荐

    计算机专业英语chapter 4PPT课件.pptx

    Queues, on the other hand, operate on a First-In-First-Out (FIFO) basis. Items are added at the end (enqueue) and removed from the front (dequeue). Queues are employed in tasks like job scheduling, ...

    华南理工大学计算机全英班算法设计实验

    (5)Understand different search strategies, such as: the Breadth-first search(BFS) ,the Depth-first search(DFS) and the Best-first search(BFS) Search strategies etc., and compare the difference of ...

    LeetCode最全代码

    * [Depth-First Search](https://github.com/kamyu104/LeetCode#depth-first-search) * [Backtracking](https://github.com/kamyu104/LeetCode#backtracking) * [Dynamic Programming]...

    Fast and Scalable Range Query Processing

    same PBtree structure if and only if the two sets have the same number of data items) and node indistinguishability (i.e., the values of PBtree nodes are completely random and have no statistical ...

    微软内部资料-SQL性能优化5

    Because the trees are balanced, finding any record requires about the same amount of resources, and retrieval speed is consistent because the index has the same depth throughout. Clustered and ...

    Dundas.Chart.for.Winform.Enterprise.v7.1.0.1812.for.VS2008

    Tree Map Chart Type - Another chart type requested by Dundas customers, the innovative Tree Map custom chart type shows any two metrics at once, using both size and color. Think of it as a "heat map ...

    Radmin自动登录器v3.0-多国语言绿色版-Release1-20150615

    RecordName,IP,Port,User,Password,Domain,ColorDepth,Updates,UnlockDesktop,Fullscreen,Nofullkbcontrol,Monitor,Sendrequest,Pbpath,Proxy,AsProxyBy,Memory,TreePath sample01,192.168.0.6,4899,user01,,,,,,,...

    acpi控制笔记本风扇转速

    upsearch until a device scope is found before executing _ADR. This allows PCI_Config operation regions to be declared locally within control methods underneath PCI device objects. Fixed a problem ...

    Radmin自动登录器v3.0

    RecordName,IP,Port,User,Password,Domain,ColorDepth,Updates,UnlockDesktop,Fullscreen,Nofullkbcontrol,Monitor,Sendrequest,Pbpath,Proxy,AsProxyBy,Memory,TreePath sample01,192.168.0.6,4899,user01,,,,,,,...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    To guarantee uniqueness, they should be based on the full path in a project's source tree. For example, the file foo/src/bar/baz.h in project foo should have the following guard: #ifndef FOO_BAR_BAZ...

    王小平版遗传算法的光盘源代码

    Basically, the code does the same things that Koza & Rice s simple LISP does and is set up to handle multiple populations as well (e.g., for co-evolution). You need to provide three modules, setup.c...

    [3planesoft屏幕保护程序合集].Premium.3D.Screensavers.iso

    Once he surf the seas, and now sleeps on the depth and has dreams of waves and wind, blow it sails. Crystal Fireplace 3D Screensaver v1.0 build 5 Find yourself in a mysterious dwelling gnomes! ...

    VB编程资源大全(英文源码 控制)

    You can easily retrieve or change the screen resolution and color depth, play audio CDs and standard wave files <END><br>44 , fsocontrol.zip This demonstration version of this control is a wrapper...

Global site tag (gtag.js) - Google Analytics