Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

递归遍历左子树和右子树

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:bool isSameTree(TreeNode* p, TreeNode* q) {if (p == NULL && q == NULL) return true;if (p == NULL && q != NULL) return false;if (p != NULL && q == NULL) return false;if (p->val == q->val) {bool x = isSameTree(p->left, q->left);bool y = isSameTree(p->right, q->right);return x&&y;}return false;}
};

转载于:https://www.cnblogs.com/pk28/p/7218111.html

100. Same Tree相关推荐

  1. LeetCode 100. Same Tree

    LeetCode 100. Same Tree Solution1: 这种弱智题提交这么多次... /*** Definition for a binary tree node.* struct Tr ...

  2. [Leetcode]100. Same Tree -David_Lin

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  3. [LeetCode] NO. 100 Same Tree

    [题目] Given two binary trees, write a function to check if they are equal or not. Two binary trees ar ...

  4. 100. Same Tree同样的树

    [抄题]: Given two binary trees, write a function to check if they are the same or not. Two binary tree ...

  5. Leet Code OJ 100. Same Tree [Difficulty: Easy]

    题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...

  6. 32/100. Binary Tree Inorder Traversal

    中序遍历二叉树. 递归法: # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x) ...

  7. 16/100. Symmetric Tree

    给定一个二叉树,看它是不是"镜像树"(瞎起的). 递归.从上往下,分别判断左右子树的根节点值是否相等,相等则继续往下递归,不等则返回False. # Definition for ...

  8. [LeetCode]: 100: Same Tree

    题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...

  9. [leetcode]100.Same Tree

    题目 Given two binary trees, write a function to check if they are the same or not. Two binary trees a ...

最新文章

  1. 2015/Province_C_C++_C/8/饮料换购
  2. Topk 问题详解及代码和数据分析
  3. 用aria2c下迅雷离线资源
  4. [转载]使用命名管道实现进程间通信
  5. MyBatis + MVC 获取 UI 参数的几种方法(二)
  6. lintcode 中等题:Single number III 落单的数III
  7. JAVA企业级应用服务器之TOMCAT实战
  8. python 机器学习——从感知机算法到各种最优化方法的应用(python)
  9. [Serializable]C#中的对象序列化
  10. PLC项目增频减频部分流程图(修改版)
  11. java 如何去掉http debug日志_Java高手如何搭建高效易用的日志系统
  12. robotstudio工作站建立
  13. pv 、uv、ip、vv、cv分别是什么
  14. java 锟斤 解决乱码_java eclipse 开发中文乱码锟斤拷小锟斤拷锟
  15. javaWeb(b站狂神说)
  16. R语言入门第一集 R语言、RTools、RStudio的简介、安装与使用
  17. android网络传输唤醒系统,Android手机唤醒群晖NAS系统
  18. 转载:关于Vivado综合选项——Out of context per IP和Gobal
  19. c# UTC时间和本地时间转换(北京时间)
  20. ADI DSP的寄存器详细说明在哪里?

热门文章

  1. css规则可以放在云上,CSS中!important规则的使用方法
  2. c语言调用go函数,C中调用go中的回调函数
  3. php 实例 规范,PHP开发规范实例详解
  4. 清除浏览器缓存之后为什么还是显示旧的html页面_Web缓存控制策略详解
  5. java changestr,java change
  6. java s1_转!!Java 基础面试题的剖析: short s1=1;s1 = s1 +1 报错? s1+=1 呢
  7. 一些图形学中的数学应用
  8. 人工神经网络——笔记摘抄2
  9. 机器学习两种距离——欧式距离和马氏距离
  10. Ubuntu中python调用SimpleITK来显示图像