leetcode(100):Same Tree

news/2024/7/4 15:08:23 标签: leetcode

题目:Given two binary trees, write a function to check if they are the same or not.

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

题目分析:判断二棵树是否相同,需要对树来进行遍历,然后判断每个节点的值是否相同,来判断树是否相同。
在这里,我先考虑的是先序遍历,也就是判断先访问根节点,然后在访问左子节点,右子节点。
python代码如下:

# 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 == None and q == None:
            return True
        if p != None and q == None:
            return False
        if p == None and q != None:
            return False
        if p.val != q.val:
            return False
        if self.isSameTree(p.left, q.left) == False:
            return False
        if self.isSameTree(p.right, q.right) == False:
            return False
        return True

下面来看一下大佬的做法:

def isSameTree(self, p, q):
    if p and q:
        return p.val == q.val and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
    return p is q

在这个答案中,我们可以学到的是is语句的用法,对于一个变量而言,有3个属性,id,value,type,顾名思义,id就是变量的地址,value就是变量的值,而type就是变量的类型,如:

>>a = 1
>>b = 1
>>a is b
true
>>a = 1
>>b = 1.0
>>a is b
false
>>a == b
true

is要求二个变量完全相同,==的加强版,既要value相同,又要type相同,而value相同,表明了==的关系,所以成立的条件会相对而言比较宽松。

其实大佬的逻辑与我的逻辑是一致的,只是水平的差异,所以人家的语句很简练,而我很冗长,没关系,好好阅读大佬的程序就好。
对于这个问题,需要注意的是,对于None类型的节点而言,是没有.val的操作的。
其实最主要的是寻找相同的重复的模式,对于二个节点,无外乎四种情况,t1为None,t2为None,继续判断,t1为非None,t2为None,返回False退出即可,t1是None,t2为非None,返回False退出即可,t1是非None,t2为非None,需要比较二者的Val值,然后再依次判断左子节点的i情况,右子节点的情况。

从此题目得到的知识点:
第一,简化的书写,对于判断p, q是否是空节点的写法,直接写即可,可以不写==
即判断,p != None and q != None,写成p and q
p != None写成if p:同等类比的来写
第二,对于判断如果二者均为空则返回true,一个为空则返回false,采用is来实现


http://www.niftyadmin.cn/n/1798849.html

相关文章

裁剪图片中遇到问题,怎么解决?

2019独角兽企业重金招聘Python工程师标准>>> 问题代码: Intent cropIntent new Intent("com.android.camera.action.CROP");cropIntent.setType("image/*");cropIntent.putExtra("crop", "true");cropIntent.p…

POJ 1847 最短路问题 dijkstra算法的实现

首先自己练习了一下实现dijkstra算法,可以把dj算法与prim算法对比记忆,要理解pre数组、min数组、V标记数组的含义! //单源最短路径,dijkstra算法,邻接阵形式,复杂度O(n^2)//求出源s到所有点的最短路经,传入图的顶点数n,(有向)邻接矩阵mat//返回到各点最…

leetcode(101):Symmetric Tree

题目:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). 对于题目的分析:这个题目要求判断给定的这棵树是不是关于根节点对称。对于根节点而言,首先需要判断根节点是不是为None形式&#…

消除云界限——NetApp更新高端存储

云本身就是无形的、灵动的。但是借用了“云”概念的计算却非要分清到底是公有、私有还是混合。 如今,人们又在想方设法消除公有云、私有云和混合云之间的界限,让数据和应用能够在“自由云域”中畅行。无论是公有云、私有云还是混合云,它们对于…

linux上部署nodejs

linux上部署nodejs 环境:# lsb_release -aLSB Version: :core-4.0-amd64:core-4.0-ia32:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-ia32:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-ia32:printing-4.0-noarchDistributor ID: CentOSDescript…

leetcode(617):Merge Two Binary Trees

题目要求: Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge them into a new binary tree. The merge rule is that if two no…

POJ 1247 数组首尾求和判定相等 枚举法

此题把题意弄懂后就很容易&#xff0c;S顺时针走&#xff0c;E逆时针走&#xff0c;确定一个分割点该处两人相遇时途经的客人数之和相等&#xff0c;直接枚举。 #include <iostream> using namespace std; //s顺时针走&#xff0c;e逆时针走&#xff0c;问他们在哪里相遇…

开源 免费 java CMS - FreeCMS1.8 互动信件

2019独角兽企业重金招聘Python工程师标准>>> 项目地址&#xff1a;http://code.google.com/p/freecms/ 互动信件 1. 部门信件管理 部门信件指接收人为部门的信件,从左侧管理菜单点击部门信件进入。 admin可以管理所有部门信件&#xff0c;其他用户可以管理自己…