/*
* @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—集合框架 List和Set的contains()以及Map的containsKey()、containsValue()

    如何判断集合中是否存在某个元素——contains() 1.List的contains(obj)方法 实际上,List调用contains(Object obj)方法时,会遍历List中的每一个元素, ...

  2. AOP的实现

    AOP基于xml配置方式实现 Spring基于xml开发AOP 定义目标类(接口及实现类) /** * 目标类 */ public interface UserService { //业务方法 pub ...

  3. 【Leetcode】【Easy】Remove Duplicates from Sorted Array

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  4. javascript正则表达式 - 学习笔记

    JavaScript 正则表达式 学习笔记 标签(空格分隔): 基础 JavaScript 正则表达式是用于匹配字符串中字符组合的模式.在javascript中,正则表达式也是对象.这些模式被用于Re ...

  5. 12C RAC 常用检查命令,持续总结中

    grid: olsnodes -s列出集群中节点crsctl check cluster -all检查几圈状态crsctl check clustercrsctl check crs 检查当前节点sr ...

  6. python:线程进阶

    1,守护线程 import time from threading import Thread def func(): print('开始执行子线程') time.sleep(3) print('子线 ...

  7. HDU 2586 How far away ?【LCA】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=2586 How far away ? Time Limit: 2000/1000 MS (Java/Oth ...

  8. 用java语言编写的简单二叉树

    package com.cjonline.foundation.evisa; public class TestTree { private int data=-1; private TestTree ...

  9. flume ng 1.3 安装(转)

    http://blog.csdn.net/hijk139/article/details/8308224 业务系统需要收集监控系统日志,想到了hadoop的flume.经过试验,虽说功能不算足够强大, ...

  10. socket 代码实例

    ​ 1. TCP SOCKET 客户端: #!/usr/bin/env python # -*-coding:utf-8 -*- import socket HOST = 'localhost' PO ...