/*
* @lc app=leetcode.cn id=100 lang=c
*
* [100] 相同的树
*
* https://leetcode-cn.com/problems/same-tree/description/
*
* algorithms
* Easy (51.47%)
* Total Accepted: 16K
* Total Submissions: 31K
* Testcase Example: '[1,2,3]\n[1,2,3]'
*
* 给定两个二叉树,编写一个函数来检验它们是否相同。
*
* 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
*
* 示例 1:
*
* 输入: 1 1
* ⁠ / \ / \
* ⁠ 2 3 2 3
*
* ⁠ [1,2,3], [1,2,3]
*
* 输出: true
*
* 示例 2:
*
* 输入: 1 1
* ⁠ / \
* ⁠ 2 2
*
* ⁠ [1,2], [1,null,2]
*
* 输出: false
*
*
* 示例 3:
*
* 输入: 1 1
* ⁠ / \ / \
* ⁠ 2 1 1 2
*
* ⁠ [1,2,1], [1,1,2]
*
* 输出: false
*
*
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
if(p == NULL && q == NULL) return true;
if(p != NULL && q != NULL){
if(p -> val != q -> val) return false;
else{
return (isSameTree(p -> left, q -> left) && isSameTree(p -> right, q -> right));
}
}
else return false;
}

一般来说对树的操作,用递归法比较简单,第一个判断是否都为空,当都不为空的情况下判断值是否相等。不相等返回false。相等的话,进行递归,只有当左孩子和右孩子都满足条件的时候返回true,否则就是false了。

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

python:

# 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, q): """ :type p: TreeNode :type q: TreeNode :rtype: bool """
def issamenode(a,b): if a==None and b==None: return True if (a and b) == None: return False #注意加括号 if a.val !=b.val: return False return issamenode(a.left,b.left) and issamenode(a.right,b.right) return issamenode(p,q)

Leecode刷题之旅-C语言/python-100相同的树的更多相关文章

  1. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  2. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  3. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  4. Leecode刷题之旅-C语言/python-7.整数反转

    /* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...

  5. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  6. Leecode刷题之旅-C语言/python-326 3的幂

    /* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...

  7. Leecode刷题之旅-C语言/python-263丑数

    /* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...

  8. Leecode刷题之旅-C语言/python-383赎金信

    /* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...

  9. Leecode刷题之旅-C语言/python-349两整数之和

    /* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...

随机推荐

  1. java面试题之----spring与struts2的比较

    我们从以下几个维度来区分两者的概念: 1.  机制:spring mvc的入口是servlet,而struts2是filter.     补充几点知识:     < Filter 实现javax ...

  2. css:改变滚动条样式

    以下亲测谷歌内核的浏览器有用,微软和火狐无效 body::-webkit-scrollbar {/*滚动条整体样式*/ width: 5px; /*高宽分别对应横竖滚动条的尺寸*/ height: 1 ...

  3. 关于main函数的参数,argc,argv的内部机制

    偶尔对main函数的参数感兴趣,写了个程序验证. int main(int argc,char **argv) 首先,解释两点: 第一 .系统将参数列表,即我们在shell下输入的命令,存储到一个一维 ...

  4. I want to be a Great Web Front-end Developer

    有时觉得特别的浮躁,可能是每天春运般的挤地铁,随处可见因为一点小磕小蹭吹胡子瞪眼睛的人,可能是身边的人貌似一下子都好有钱,买房买车或者买第N套房. 很想静下来心寻找到自己inner pace,但是忽然 ...

  5. 推荐一个很好用的脚本session snapper

    源网址http://tech.e2sn.com/oracle-scripts-and-tools/session-snapper 内容: If you want to just download Sn ...

  6. Orchard Core 文档翻译 (二)代码生成模板 Code Generation Templates

    Code Generation Templates 翻译原文:https://www.cnblogs.com/Qbit/p/9746457.html转载请注明出处 Orchard Core Templ ...

  7. VMware,win7与linux centos6.4文件互传,linux下挂载windows共享文件夹,vmware tools安装方法

    本方法是以win7,VMware9.0.1 ,centos6.4为基础实验的. 对于linux的初级使用阶段,都会Windows中使用linux虚拟机VMWare或者其它的.在Windows与linu ...

  8. JQuery datatables 标题和内容居中显示

    1.如题,使用到了强大的表格插件datatables,要使标题和内容都居中显示,只需要在jsp引入css,写上如下内容即可: /*qiulinhe:2016年11月7日13:48:26*/ /* da ...

  9. Android开发学习之TabView选项卡具体解释 -- 基于Android4.4

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/he90227/article/details/24474197 直接上代码 -- 基于Android ...

  10. 【[HAOI2011]Problem c】

    好题啊 先考虑一些如何判掉无解的情况 我们开一个桶,存一下每个编号有多少个人必须选,之后做一个后缀和,之后我们扫一遍,如果一旦有一个后缀和\(pre[i]\)超过\(n-i+1\)就不合法了,因为我们 ...