Total Accepted: 83663 Total Submissions: 200541 Difficulty: Easy

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.

首先想到的是递归法,判断过程为依次遍历每一个点如果有:

  1. 如果都为null或是都不为null且值相等返回true
  2. 一个为Null另一个不为Null返回false
  3. 两个都不为null但值不相等返回false

代码如下:

class TreeNode{
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class Solution {
public static boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null&&q==null) return true; //同时到达叶子节点
else if(p==null||q==null) return false; //不同时到达叶子则不相同
if(p.val!=q.val) return false; //节点值不同则不相同
else return isSameTree(p.left, q.left)&&isSameTree(p.right, q.right); //递归 }
//测试
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeNode t1=new TreeNode(1);
TreeNode nodeB=new TreeNode(2);
t1.left=nodeB; TreeNode t2=new TreeNode(1);
TreeNode nodeB1=new TreeNode(2);
t2.left=nodeB1;
System.out.println(isSameTree(t1,t2));
} }

LeetCode (65):Same tree的更多相关文章

  1. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

  2. Leetcode 101 Symmetric Tree 二叉树

    判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...

  3. LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...

  4. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...

  5. C#版 - Leetcode 65. 有效数字 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ...

  6. leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II

    leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  7. [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  8. [LeetCode] 429. N-ary Tree Level Order Traversal_ Easy

    Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  9. LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)

    翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...

随机推荐

  1. 160311、mybatis sql语句中转义字符

    问题: 在mapper  ***.xml中的sql语句中,不能直接用大于号.小于号要用转义字符 解决方法:   1.转义字符串 小于号    <    < 大于号    >    & ...

  2. FZU 2144 Shooting Game (贪心区域划分)

    Problem 2144 Shooting Game Accept: 370 Submit: 1902 Time Limit: 1000 mSec Memory Limit : 32768 KB Pr ...

  3. FZU 2140 Forever 0.5(找规律,几何)

    Problem 2140 Forever 0.5 Accept: 371 Submit: 1307 Special Judge Time Limit: 1000 mSec Memory Limit : ...

  4. 适配器模式(Adpater)

    一.适配器模式介绍 适配器模式:将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的类可以一起工作. 例如: NBA中的球员来自不同国家,而世界标准语言 ...

  5. 用于把List<Object>转换成Map<String,Object>形式

    /** * 用于把List<Object>转换成Map<String,Object>形式,便于存入缓存 * @author zhang_bo * @param keyName ...

  6. MongoDB 使用 ObjectId 代替时间

    An ObjectId is a 12-byte unique identifier consisting of: a 4-byte value representing the seconds si ...

  7. Clairewd’s message--hdu4300(Next数组的运用)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4300 题意就是给你26的字母的加密方式,然后又给了一个串s1是包含加密后的和没有加密的但是没有加密的可 ...

  8. 【Servlet】把文件写到Respond输出流里面供用户下载

    本文区分于<[Jsp]把Java写到Respond输出流里面供用户下载>(点击打开链接)把原本该打印到控制台的内容,直接打印到一个文本文件txt中给用户下载. 实际上是<[Strut ...

  9. 启动tomcat时为tomcat指定JDK

    背景:服务器环境:JDK1.7,Tomcat8 需求: 需要在Tomcat8部署项目,该项目需要运行在JDK1.8 将Tomcat8和JDK1.8上传至服务器,然后解压在指定目录下. 为tomcat指 ...

  10. 1. testNG+Maven 环境搭建

    一:使用的工具 : TestNG 6.9.10 Maven 3.5 IDEA 二:创建maven项目,在pom.xml添加依赖 <?xml version="1.0" enc ...