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. 解决VMware Workstation虚拟机不能联网的解决办法

    在windows服务中查看,以下几个服务是否正常开启,没有就开启

  2. Python 常用资源

    Python:https://www.python.org/ftp/python/ Django:https://www.djangoproject.com/download/

  3. POJ3272 Cow Traffic

    题目链接:http://poj.org/problem?id=3272 题目意思:n个点m条边的有向图,从所有入度为0的点出发到达n,问所有可能路径中,经过的某条路的最大次数是多少.边全是由标号小的到 ...

  4. /proc/iomem和/proc/ioports对应的fops

    /proc/iomem和/proc/ioports对应的fops static int __init ioresources_init(void) {     struct proc_dir_entr ...

  5. where both the key and the value are strings

    Environment Variables (The Java™ Tutorials > Essential Classes > The Platform Environment) htt ...

  6. Apache Samza - Reliable Stream Processing atop Apache Kafka and Hadoop YARN

    http://engineering.linkedin.com/data-streams/apache-samza-linkedins-real-time-stream-processing-fram ...

  7. Linux 搭建Git服务器

    安装Git yum install -y git git --version 创建 Git 用户 sudo adduser git // 设置密码 passwd git 导入公钥 find / -na ...

  8. python基础-第五篇-5.4正则表达式

    正则基础知识 正则表达式是通过调用re模块实现的 在python里,正则表达式处理对象为字符串,所以正则里方法和字符串的方法有很多相似的地方:re.findall和find,re.split和spli ...

  9. 前端开发 - JQuery - 下

    二十五.jquery的事件 <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  10. mysql总结思维导图

    mysql总结思维导图.脑图 先整理了一个思维导图出来,到时候再继续补充并且深入挖掘一下,再写博文. 另外,看了很多优秀的博文,在这里先mark一下. https://www.cnblogs.com/ ...