题目

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     public boolean isSameTree(TreeNode p, TreeNode q) {
 2         if(p==null&&q==null)
 3             return true;
 4         
 5         if(p==null&&q!=null)
 6             return false;
 7         
 8         if(p!=null&&q==null)
 9             return false;
             
         if(p.val!=q.val)
             return false;
         boolean isleftsame = isSameTree(p.left,q.left);
         if(!isleftsame)
             return false;
             
         boolean isrightsame = isSameTree(p.right,q.right);
         if(!isrightsame)
             return false;
         
         return true;
         
     }

对上述代码更简洁的写法是:

     public boolean isSameTree(TreeNode p, TreeNode q) {
         if(p == null&&q == null)
             return true;
         if(p == null||q == null)
             return false;
         return (p.val == q.val) && isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
     }

Same Tree leetcode java的更多相关文章

  1. Symmetric Tree leetcode java

    问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...

  2. Recover Binary Search Tree leetcode java

    题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...

  3. Validate Binary Search Tree leetcode java

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

  4. Balanced Binary Tree leetcode java

    题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

  5. Convert Sorted Array to Binary Search Tree leetcode java

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  6. Minimum Depth of Binary Tree leetcode java

    题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...

  7. Maximum Depth of Binary Tree leetcode java

    题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

  8. Convert Sorted List to Binary Search Tree leetcode java

    题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height ...

  9. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

随机推荐

  1. NPE是什么

    在编程中出现的空指针异常 Map类集合K/V能不能存储null值的情况,如下表格: remove元素要使用Iterator方式,如果并发操作,需要对Iterator对象加锁. Arrays.asLis ...

  2. 八皇后II

    用一个数组state记录已经选择的每一行皇后所在的位置,DFS count = 0 N = 8 state = [0]*N def dfs(row): global count for col in ...

  3. JAVA内存泄漏解决办法

    JVM调优工具 Jconsole,jProfile,VisualVM Jconsole : jdk自带,功能简单,但是可以在系统有一定负荷的情况下使用.对垃圾回收算法有很详细的跟踪.详细说明参考这里 ...

  4. Python 面向对象编程——类定义与对象

    <类定义与对象声明> 面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对 ...

  5. 手机html根据手机分辨率网页文字大小自适应

    问题:不同手机型号屏幕尺寸大不相同,导致同样的文字,有的显示一行,有的显示多行. 通过查资料和自己的尝试解决:网页开发习惯的px单位,手机html开发不适用. 源代码如下: <!DOCTYPE ...

  6. Linux怎么开启ssh

    一.查看ssh开启状态 service ssh status 这是已经开启了的状态 二.如果没有开启  键入以下命令开启 service ssh start 三.开启后如果不能利用xshell远程访问 ...

  7. hdu 4025 Equation of XOR 状态压缩

    思路: 设: 方程为 1*x1 ^ 1*x2 ^ 0*x3 = 0; 0*x1 ^ 1*x2 ^ 1*x3 = 0; 1*x1 ^ 0*x2 ^ 0*x3 = 0 把每一列压缩成一个64位整数,因为x ...

  8. sgu 176 上下界网络流最小可行流带输出方案

    算法步骤: 1. 先将原图像最大可行流那样变换,唯一不同的是不加dst->src那条边来将它变成无源无汇的网络流图.直接跑一边超级源到超级汇的最大流. 2. 加上刚才没有加上的那条边p 3. 再 ...

  9. Bootstrap_CSS概览

    在这一章中,我们将讲解 Bootstrap 底层结构的关键部分,包括我们让 web 开发变得更好.更快.更强壮的最佳实践. HTML 5 文档类型(Doctype) Bootstrap 使用了一些 H ...

  10. 推荐一个简洁优雅的博客系统,farbox

    这是我用farbox搞的一个博客:http://www.jsnull.com/ 特点: 1.无数据库,数据存在dropbox里,需要自己注册一个dropbox帐号 2.静态文本文件即是文章,可以在任何 ...