1. /*
  2. * @lc app=leetcode.cn id=100 lang=c
  3. *
  4. * [100] 相同的树
  5. *
  6. * https://leetcode-cn.com/problems/same-tree/description/
  7. *
  8. * algorithms
  9. * Easy (51.47%)
  10. * Total Accepted: 16K
  11. * Total Submissions: 31K
  12. * Testcase Example: '[1,2,3]\n[1,2,3]'
  13. *
  14. * 给定两个二叉树,编写一个函数来检验它们是否相同。
  15. *
  16. * 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
  17. *
  18. * 示例 1:
  19. *
  20. * 输入: 1 1
  21. * ⁠ / \ / \
  22. * ⁠ 2 3 2 3
  23. *
  24. * ⁠ [1,2,3], [1,2,3]
  25. *
  26. * 输出: true
  27. *
  28. * 示例 2:
  29. *
  30. * 输入: 1 1
  31. * ⁠ / \
  32. * ⁠ 2 2
  33. *
  34. * ⁠ [1,2], [1,null,2]
  35. *
  36. * 输出: false
  37. *
  38. *
  39. * 示例 3:
  40. *
  41. * 输入: 1 1
  42. * ⁠ / \ / \
  43. * ⁠ 2 1 1 2
  44. *
  45. * ⁠ [1,2,1], [1,1,2]
  46. *
  47. * 输出: false
  48. *
  49. *
  50. */
  51. /**
  52. * Definition for a binary tree node.
  53. * struct TreeNode {
  54. * int val;
  55. * struct TreeNode *left;
  56. * struct TreeNode *right;
  57. * };
  58. */
  59. bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
  60. if(p == NULL && q == NULL) return true;
  61. if(p != NULL && q != NULL){
  62. if(p -> val != q -> val) return false;
  63. else{
  64. return (isSameTree(p -> left, q -> left) && isSameTree(p -> right, q -> right));
  65. }
  66. }
  67. else return false;
  68. }

一般来说对树的操作,用递归法比较简单,第一个判断是否都为空,当都不为空的情况下判断值是否相等。不相等返回false。相等的话,进行递归,只有当左孩子和右孩子都满足条件的时候返回true,否则就是false了。

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

python:

  1. # Definition for a binary tree node.
  2.  
  3. # class TreeNode:
  4.  
  5. # def __init__(self, x):
  6.  
  7. # self.val = x
  8.  
  9. # self.left = None
  10.  
  11. # self.right = None
  12.  
  13. class Solution:
  14. def isSameTree(self, p, q):
  15.  
  16. """
  17.  
  18. :type p: TreeNode
  19.  
  20. :type q: TreeNode
  21.  
  22. :rtype: bool
  23.  
  24. """
  25. def issamenode(a,b):
  26.  
  27. if a==None and b==None: return True
  28.  
  29. if (a and b) == None: return False #注意加括号
  30.  
  31. if a.val !=b.val:
  32.  
  33. return False
  34.  
  35. return issamenode(a.left,b.left) and issamenode(a.right,b.right)
  36.  
  37. 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. 每天一个linux命令-uname,输出操作系统信息(内核版本、硬件架构32位/64位等)

      uname命令:主要用于输出一组操作系统的信息. 这个命令比较简单也比较有意思.为什么这么说呢?输入--help后看看效果,一目了然,简单吧: 首先输出完整信息: 我们再依次来输出一下: 最后,我 ...

  2. VC简单嵌入汇编

    转自:http://blog.csdn.net/arcsinsin/article/details/8126473 内嵌汇编的使用方法是: __asm {        语句 } 你可以把它插入程序中 ...

  3. js:JSON对象与JSON字符串转换

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式. 同时,JSON是 JavaScript 原生格式,这 ...

  4. Facebook 新开源了 2 个东西,一个语音识别系统(wav2letter++)和一个机器学习库(flashlight)

    Open sourcing wav2letter++, the fastest state-of-the-art speech system, and flashlight, an ML librar ...

  5. JS interview loop code

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

  6. scrum3

    首先我一直做的是框架的设计,但不同的是这次我们整合完善了这个软件目前的所有需求也定义好了它的大题框架,总的来说设计部分已经结束,现在也就是本次冲刺,我们将重点进行整个软件的数据库编程环节,也就是用SQ ...

  7. table中设置tr行间距

    CSS border-collapse 属性设置表格的边框是否被合并为一个单一的边框 值 描述 separate 默认值.边框会被分开.不会忽略 border-spacing 和 empty-cell ...

  8. PIL 图像字符画绘制

    from PIL import Image ascii_char = list('"$%_&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]? ...

  9. C# DataSet.Designer.cs

    今天在做项目的时候,发现一个很奇葩的问题,VS 中DataSet数据集的问题Dataset数据集更新,在保存后原有的Dataset.Designer.cs不变,又增加一个新的Dataset1.Desi ...

  10. P1272

    P1272 重建道路 题目描述 一场可怕的地震后,人们用N个牲口棚(1≤N≤150,编号1..N)重建了农夫John的牧场.由于人们没有时间建设多余的道路,所以现在从一个牲口棚到另一个牲口棚的道路是惟 ...