问题描述:

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.

Example 1:

Input:     1         1
/ \ / \
2 3 2 3 [1,2,3], [1,2,3] Output: true

Example 2:

Input:     1         1
/ \
2 2 [1,2], [1,null,2] Output: false

Example 3:

Input:     1         1
/ \ / \
2 1 1 2 [1,2,1], [1,1,2] Output: false

思路:

考虑递归解法,将大问题化解为无数个类似的小问题

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
if p == None and q ==None:
return True
elif p == None or q ==None or p.val != q.val:
return False
if p.val == q.val :
return self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)

Python3解leetcode Same Tree的更多相关文章

  1. Python3解leetcode N-ary Tree Level Order Traversal

    问题描述: Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to ...

  2. Python3解leetcode Symmetric Tree

    问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...

  3. Python3解leetcode Binary Tree Paths

    问题描述: Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. E ...

  4. Python3解leetcode Binary Tree PathsAdd DigitsMove Zeroes

    问题描述: Given an array nums, write a function to move all 0's to the end of it while maintaining the r ...

  5. Python3解leetcode Binary Tree PathsAdd Digits

    问题描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...

  6. Python3解leetcode Same TreeBinary Tree Level Order Traversal II

    问题描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...

  7. Python3解leetcode Average of Levels in Binary Tree

    问题描述: Given a non-empty binary tree, return the average value of the nodes on each level in the form ...

  8. Python3解leetcode Subtree of Another Tree

    问题描述: Given two non-empty binary trees s and t, check whether tree t has exactly the same structure ...

  9. Python3解leetcode Lowest Common Ancestor of a Binary Search Tree

    问题描述: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in ...

随机推荐

  1. rtems 4.11 console驱动 (arm, beagle)

    console驱动框架主要文件是 c/src/lib/libbsp/shared/console.c,驱动的入口是 console_initialize()主要作用是初始化BSP提供的全局变量 Con ...

  2. 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 a b c e s f c s a d e e 矩阵中包含一条字符串"bccced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中

    // test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  3. linux uart驱动——uart platfrom 注册(三)

    一:注册platform device 注册一个platfrom device一般需要初始化两个内容,设备占用的资源resource和设备私有数据dev.platfrom_data.设备的resour ...

  4. WPF DataGrid 获取当前行某列值

    [0]是指当前行第1列的单元格位置 注意:DataRowView要求必须引用System.Data命名空间 方法一: DataRowView mySelectedElement = (DataRowV ...

  5. EntityFramwork 查询

    EntityFramwork 查询 1.简单查询: SQL: SELECT * FROM [Clients] WHERE Type=1 AND Deleted=0 ORDER BY ID EF: // ...

  6. 基于live555实现的RTSPServer对底层进行性能优化的方法

    在博客<EasyIPCamera高性能摄像机RTSP服务器RTSPServer解决方案>我介绍了基于live555实现的一套RTSPServer功能组件,当时开发者经过几个月的调试,已经将 ...

  7. C#Panel 控件的使用

    Windows 窗体 Panel 控件用于为其他控件提供可识别的分组.通常,使用面板按功能细分窗体.例如,可能有一个订单窗体,它指定邮寄选项(如使用哪一类通营承运商).将所有选项分组在一个面板中可向用 ...

  8. 九度OJ 1042:Coincidence(公共子序列) (DP)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2303 解决:1241 题目描述: Find a longest common subsequence of two strings. 输入 ...

  9. Chernoff-Hoeffding inequality -- Chernoff bounds, and some applications

    https://www.cs.utah.edu/~jeffp/teaching/cs5955/L3-Chern-Hoeff.pdf [大数据-通过随机过程降维 ] When dealing with ...

  10. Grunt学习笔记【6】---- grunt-contrib-requirejs插件详解

    本文主要讲如何使用Grunt实现RequireJS文件压缩. 一 说明 ES6出来前,RequireJS是JavaScript模块化最常用的方式之一.对于使用RequireJS构建的项目,要实现打包压 ...