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

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

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
} else if (p == null && q != null || p != null && q == null) {
return false;
} else if (p.val != q.val) {
return false;
}
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}

[LC] 100. Same Tree的更多相关文章

  1. 100. Same Tree(C++)

    100. Same Tree Given two binary trees, write a function to check if they are equal or not. Two binar ...

  2. LeetCode Javascript实现 100. Same Tree 171. Excel Sheet Column Number

    100. Same Tree /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; ...

  3. 100.Same Tree(E)

    100. same tree 100. Same Tree Given two binary trees, write a function to check if they are the same ...

  4. <LeetCode OJ> 100. Same Tree

    100. Same Tree Total Accepted: 100129 Total Submissions: 236623 Difficulty: Easy Given two binary tr ...

  5. LeetCode 100. Same Tree (判断树是否完全相同)

    100. Same Tree Given two binary trees, write a function to check if they are the same or not. Two bi ...

  6. leetcode 100. Same Tree、101. Symmetric Tree

    100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...

  7. 100. Same Tree同样的树

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

  8. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  9. LeetCode之100. Same Tree

    ------------------------------------------ 递归比较即可 AC代码: /** * Definition for a binary tree node. * p ...

随机推荐

  1. 刷题33. Search in Rotated Sorted Array

    一.题目说明 这个题目是33. Search in Rotated Sorted Array,说的是在一个"扭转"的有序列表中,查找一个元素,时间复杂度O(logn). 二.我的解 ...

  2. Python基础学习一

    Python基础学习一 1.变量与常量 变量名:大小写英文.数字.下划线的组合,数字不能开头 常量名:习惯上常量用大写字母命名,例如"PI" 2.多行输出 转义符:反斜杠(),如果 ...

  3. MySQL的InnoDB的幻读问题

    MySQL InnoDB事务的隔离级别有四级,默认是“可重复读”(REPEATABLE READ). 未提交读(READ UNCOMMITTED).另一个事务修改了数据,但尚未提交,而本事务中的SEL ...

  4. vue每次运行起来端口不一致问题

    原因:portfinder新发布的版本异常 解决方案:npm install portfinder@1.0.21

  5. 寒假day01-Spring框架

    1.什么是Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE D ...

  6. Tomcat8 启动报错

    Tomcat8启动报错: java.lang.NoSuchMethodError:javax.servlet.ServletContext.getClassLoader 在网上搜索后,发现此类问题大都 ...

  7. tensorflow实现sphereFace网络(20层CNN)

    #coding:utf-8 import tensorflow as tf from tensorflow.python.framework import ops import numpy as np ...

  8. NOIp2017TG解题报告

    NOIp2018RP++! 虽然没去但还得写写QAQ D1T1 : 小凯的疑惑 数学题 手推几组数据然后发现规律 \(Ans = (a-1)(b-1)+1\) AC in 1minite D1T2 : ...

  9. Spring AOP中使用args表达式访问目标方法的参数

    Spring AOP 的使用过程理解 首先,aop的使用场景介绍: 1.处理一些通用的非功能性的需求,不影响业务流程,比如说打印日志.性能统计.推送消息等: 2.aop无法拦截static.final ...

  10. Web前端学习方向

    第一部分 HTML 第一章 职业规划和前景 职业方向规划定位: web前端开发工程师 web网站架构师 自己创业 转岗管理或其他 web前端开发的前景展望: 未来IT行业企业需求最多的人才 结合最新的 ...