Leecode刷题之旅-C语言/python-100相同的树
/*
* @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相同的树的更多相关文章
- Leecode刷题之旅-C语言/python-1.两数之和
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- Leecode刷题之旅-C语言/python-28.实现strstr()
/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...
- Leecode刷题之旅-C语言/python-7.整数反转
/* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...
- Leecode刷题之旅-C语言/python-434 字符串中的单词数
/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...
- Leecode刷题之旅-C语言/python-326 3的幂
/* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...
- Leecode刷题之旅-C语言/python-263丑数
/* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...
- Leecode刷题之旅-C语言/python-383赎金信
/* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...
- Leecode刷题之旅-C语言/python-349两整数之和
/* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...
随机推荐
- 转载: Centos7 升级python3,解决升级后不兼容问题
Centos7配置更新国内yum源 http://blog.csdn.net/qingguiyu/article/details/50721956 Centos7 升级python3,解决升级后不兼容 ...
- ubuntu 可以加速播放的播放器SMPlayer 16.4安装
直接贴命令 sudo apt-add-repository ppa:rvm/smplayer sudo apt-get update sudo apt-get install smplayer smp ...
- 【洛谷5294】[HNOI2019] 序列(主席树维护单调栈+二分)
点此看题面 大致题意: 给你一个长度为\(n\)的序列\(A\),每次询问修改一个元素(只对当前询问有效),然后让你找到一个不下降序列\(B\),使得这两个序列相应位置之差的平方和最小,并输出这个最小 ...
- Spark Streamming 基本输入流I(-) :File/Hdfs
Spark Streamming 基本输入流I(-):从文件中进行读取 文件读取1:本地文件读取 这里我只给出实现代码及操作步骤 1.在本地目录下创建目录,这里我们创建目录为~/log/ 2.然后手动 ...
- (第五场)J plan 【贪心】
题目链接:https://www.nowcoder.com/acm/contest/143/J 题目描述 There are n students going to travel. And hotel ...
- maven 项目运行找不到类
其实maven 仓库有jar包,可能未编译或破损, 在本地仓删除它,然后maven install
- 【洛谷P1978】 集合
集合 题目链接 显然,我们是要把数据先排序的, 然后从大到小枚举每个数,看是否能选上, 能选就选,不能拉倒 若能,二分查找a[i]/k,若查找成功,ans++ 将a[i]/k标记为不能选择 最后输出答 ...
- vim_preview_window
*29.2* The preview window When you edit code that contains a function call, you need to use the c ...
- detection工作
今天看到YOLO2的工作还是很不错的,效果好,关键是速度也快,已经完胜SSD了感觉. 虽然faster rcnn各方面效果都不错,但是从简单粗暴的角度考虑,SSD和YOLO真的深得我心啊. 检测模型, ...
- HTML表格和表单
<table>格式: 注意:1. 合并单元格:COLSPAN(跨列)ROWSPAN(跨行) 2.cellspacing属性定义单元格之间的间距(以像素为单位). cellpadding属性 ...