zoukankan      html  css  js  c++  java
  • 边工作边刷题:70天一遍leetcode: day 18-2

    Same Tree

    这题很简单,就是递归,但是因为有null结点,所以是否code能写得很neat是关键。具体来说,就是有一个为null另一个不为null,是false,都为null,true。所以先判断都为null的情况,如果不是,那么知道任何一个为null就返回false。之后就为都为not null了
    错误点:

    • 光想着结构比较,忘了两个对应结点值的比较了
    # 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 isSameTree(self, p, q):
            """
            :type p: TreeNode
            :type q: TreeNode
            :rtype: bool
            """
            if p is None and q is None:
                return True
            
            if p is None or q is None:
                return False
                
            if p.val!=q.val:
                return False
                
            return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
    
  • 相关阅读:
    安装redis
    memcached复制-repcached
    memcached一致性哈希及php客户端实现
    安装php
    安装mysql
    安装apache
    putty配色方案
    virtualbox下centos实现主宿互访
    安装memcached
    linux网络、性能相关命令
  • 原文地址:https://www.cnblogs.com/absolute/p/5677895.html
Copyright © 2011-2022 走看看