题目

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. Jenkins的Pipeline脚本在美团餐饮SaaS中的实践

    一.背景 在日常开发中,我们经常会有发布需求,而且还会遇到各种环境,比如:线上环境(Online),模拟环境(Staging),开发环境(Dev)等.最简单的就是手动构建.上传服务器,但这种方式太过于 ...

  2. TPS和QPS是什么,他们的区别是什么

    一.TPS:Transactions Per Second(每秒传输的事物处理个数),即服务器每秒处理的事务数.TPS包括一条消息入和一条消息出,加上一次用户数据库访问.(业务TPS = CAPS × ...

  3. ELK收集openstack日志

    1.安装jdk 每个openstack服务器需要安装jdk,我安装的版本jdk-7u71-linux-x64.rpm 2.安装.配置Elastic Search install https://dow ...

  4. [ 转载 ] Okhttp的用法

    Android中OkHttp的使用 LuckyXiang 简书作者 02018-01-18 19:04 打开App Android中OkHttp的使用 官方网站 | Javadoc 1 简介 OkHt ...

  5. [ 原创 ] Java基础6--构造函数和抽象类的性质

    构造函数的性质 // A.方法名与类名相同: // B.没有返回类型(例如return.void等):// C.不能被static.final.native.abstract和synchronized ...

  6. luoguP3871 [TJOI2010]中位数

    题目链接 luoguP3871 [TJOI2010]中位数 题解 平衡树 代码 #include<vector> #include<cstdio> #include<cs ...

  7. Curl 及 Curl的使用介绍

    Curl 简介 Curl是Linux下一个很强大的http命令行工具,其功能十分强大. 1) 二话不说,先从这里开始吧! $ curl http://www.linuxidc.com 回车之后,www ...

  8. 洛谷P2746 USACO5.1 校园网

    题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作“接受学校”).注意即使 B 在 A 学校的分发列表中, A 也不一定在 B 学校的列表中. 你要写 ...

  9. 改变手机浏览器(iPhone/Android)上文本输入框的默认弹出键盘

    iPhone/iPad和Android提供不同的的键盘输入类型,触发合适的键盘将极大地改善用户体验.   键盘类型 默认: 默认键盘的字母模式 数字: 默认键盘的数字模式,(含小数点等) 邮件: 与默 ...

  10. .NET面试宝典-高级2

    http://blog.csdn.net/shanyongxu/article/category/6023593 对于 Web 性能优化,您有哪些了解和经验吗? 1.前端优化 (1)减少 HTTP 请 ...