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 ...
随机推荐
- 每天一个linux命令-uname,输出操作系统信息(内核版本、硬件架构32位/64位等)
uname命令:主要用于输出一组操作系统的信息. 这个命令比较简单也比较有意思.为什么这么说呢?输入--help后看看效果,一目了然,简单吧: 首先输出完整信息: 我们再依次来输出一下: 最后,我 ...
- VC简单嵌入汇编
转自:http://blog.csdn.net/arcsinsin/article/details/8126473 内嵌汇编的使用方法是: __asm { 语句 } 你可以把它插入程序中 ...
- js:JSON对象与JSON字符串转换
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式. 同时,JSON是 JavaScript 原生格式,这 ...
- Facebook 新开源了 2 个东西,一个语音识别系统(wav2letter++)和一个机器学习库(flashlight)
Open sourcing wav2letter++, the fastest state-of-the-art speech system, and flashlight, an ML librar ...
- JS interview loop code
//九九乘法表 document.write("<table width='600' border=0'>"); for(var i=1; i<=9; i++){ ...
- scrum3
首先我一直做的是框架的设计,但不同的是这次我们整合完善了这个软件目前的所有需求也定义好了它的大题框架,总的来说设计部分已经结束,现在也就是本次冲刺,我们将重点进行整个软件的数据库编程环节,也就是用SQ ...
- table中设置tr行间距
CSS border-collapse 属性设置表格的边框是否被合并为一个单一的边框 值 描述 separate 默认值.边框会被分开.不会忽略 border-spacing 和 empty-cell ...
- PIL 图像字符画绘制
from PIL import Image ascii_char = list('"$%_&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]? ...
- C# DataSet.Designer.cs
今天在做项目的时候,发现一个很奇葩的问题,VS 中DataSet数据集的问题Dataset数据集更新,在保存后原有的Dataset.Designer.cs不变,又增加一个新的Dataset1.Desi ...
- P1272
P1272 重建道路 题目描述 一场可怕的地震后,人们用N个牲口棚(1≤N≤150,编号1..N)重建了农夫John的牧场.由于人们没有时间建设多余的道路,所以现在从一个牲口棚到另一个牲口棚的道路是惟 ...