题目

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. CI框架中site_url()和base_url()的区别

    背景:在使用CI框架的使用经常碰到跳转和路径方面的问题,site_url()和base_url()很容易混淆,下面来说说他们的区别! 假如你config文件里面的base_url和index_page ...

  2. PHP中双引号引起的命令执行漏洞

    前言 在PHP语言中,单引号和双引号都可以表示一个字符串,但是对于双引号来说,可能会对引号内的内容进行二次解释,这就可能会出现安全问题. 正文 举个简单例子 <?php $a = 1; $b = ...

  3. Flutter的原理及美团的实践

    导读 Flutter是Google开发的一套全新的跨平台.开源UI框架,支持iOS.Android系统开发,并且是未来新操作系统Fuchsia的默认开发套件.自从2017年5月发布第一个版本以来,目前 ...

  4. iOS 11开发教程(七)编写第一个iOS11代码Hello,World

    iOS 11开发教程(七)编写第一个iOS11代码Hello,World 代码就是用来实现某一特定的功能,而用计算机语言编写的命令序列的集合.现在就来通过代码在文本框中实现显示“Hello,World ...

  5. python opencv3 显示一张图片

    git:https://github.com/linyi0604/Computer-Vision # coding:utf8 import cv2 """ 显示一张图像 ...

  6. 使用SoapUI测试windows身份验证的webservice

    有个朋友问到用soapui测试wcf服务时如果使用windows身份验证要怎么传输凭据,于是自己试了一下.其实服务端是wcf还是webservice还是webapi都无所谓,关键是windows身份验 ...

  7. HDU.2899.Strange fuction(牛顿迭代)

    题目链接 \(Description\) 求函数\(F(x)=6\times x^7+8\times x^6+7\times x^3+5\times x^2-y\times x\)在\(x\in \l ...

  8. 小程序swiper 快速滑动闪屏

    bindchange: function(e){ if(e.detail.source == "touch") { this.setData({ current: current ...

  9. 华为S5300系列交换机V200R001SPH027升级补丁

    S5300SI-V200R001SPH027.pat 附件: 链接:https://pan.baidu.com/s/1ulE0j5Rp4xMkAaOjNGKLMA  密码:d5ze

  10. jquery的closest方法和parents方法的区别

    今天第一次看到closest方法,以前也从来没用过. 该方法从元素本身开始往上查找,返回最近的匹配的祖先元素. 1.closest查找开始于自身,parents开始于元素父级 2.closest向上查 ...