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 ...
随机推荐
- jQuery替换已存在于元素element上的事件event
<html><head><script type="text/javascript" src="/jquery/jquery.js" ...
- 【Leetcode】【Easy】Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 深度剖析hdfs原理
大数据底层技术的三大基石起源于Google在2006年之前的三篇论文GFS.Map-Reduce. Bigtable,其中GFS.Map-Reduce技术直接支持了Apache Hadoop项目的诞生 ...
- OC 方法声明使用
Person.h #import <Foundation/Foundation.h> @interface Person : NSObject { int _age; } - (void) ...
- python解析HTML之:PyQuery库的介绍与使用
本篇大部分转载于https://www.jianshu.com/p/c07f7cd1b548 先放自已自己解析techweb一个网站图片的代码 from pyquery import PyQuery ...
- Hibernate 基于外键映射的一对一关联关系随手记
//有外键的一端默认使用懒加载. //没有外键的一端不使用懒加载,而是直接将它引用的对象也一并查询出来. //没有外键列不仅有外键约束还有唯一约束,即没有外键列一端的对象不能被有外键列一端的两个对象同 ...
- 【LOJ6062】「2017 山东一轮集训 Day2」Pair(线段树套路题)
点此看题面 大致题意: 给出一个长度为\(n\)的数列\(a\)和一个长度为\(m\)的数列\(b\),求\(a\)有多少个长度为\(m\)的子串与\(b\)匹配.数列匹配指存在一种方案使两个数列中的 ...
- caffe的卷积层的乘积运算的优化
https://hal.inria.fr/file/index/docid/112631/filename/p1038112283956.pdf caffe的卷积计算的优化来自这篇paper,实际上就 ...
- 【luoguP1238】【NOIP2014】生活大爆炸版剪刀石头布
生活大爆炸版剪刀石头布 ——[传送门] 这道题可以原原本本地说得上是一道水题了,通过判断两人的出拳不同给分然后统计输出.就是对于游戏得分 ...
- oracle 基本语法(一)
1.基本语句: .查询每个部门工资最高的人的详细记录 select * from emp e,(select max(sal) max,deptno from emp group by deptno) ...