/*
* @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. 使用 richtextbox 输出程序运行信息

    private delegate void Refresh_CallBack(Color color,string text); private void ControlsRefresh(Color ...

  2. ubuntu安装google test

    google test 简称gtest,是一个C/C++的单元测试框架,它的代码在github仓库,使用起来还是挺方便的. 安装 先确保PC上有安装cmake: sudo cmake --versio ...

  3. 批量生成DDL脚本

    获取用户下所有索引脚本,用于数据迁移后重建索引: set pagesize 0set long 90000set feedback offset echo offspool get_index_ddl ...

  4. JS interview loop code

    //九九乘法表 document.write("<table width='600' border=0'>"); for(var i=1; i<=9; i++){ ...

  5. 二维数组展示到DataGridView(c#)

    窗体程序中二维数组展示到DataGridView public void TwoDArrayShowINDatagridview(string[,] arr) { DataTable dt = new ...

  6. SQL SERVER启动时间

    select sqlserver_start_time,* from sys.dm_os_sys_info

  7. 课堂练习:HTML-----------一般标签、常用标签

    []练习:一般标签.常用标签 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  8. python time模块计算程序耗时

    import time start = time.clock() end = time.clock() consume_time = end - start

  9. ueditor图片上传,网络连接错误的解决方案

    错误产生的原因是ueditor/net目录中的Uploader.cs在网站发布之后就没有了,重新上传这个文件,问题就解决了

  10. POJ 3666 Making the Grade(数列变成非降序/非升序数组的最小代价,dp)

    传送门: http://poj.org/problem?id=3666 Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total ...