题目

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. 59. 螺旋矩阵 II

    给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, ...

  2. 预备作业02:体会做中学(Learning By Doing)

    1.很惭愧,我并没有什么技能能强过大家. 2...... 3.我觉得培养一个技能,必须要通过勤勉的练习,认真的学习,还有不断地结合实践. 4.我觉得我学习<程序设计与数据结构>之后应该对程 ...

  3. HP电脑的增霸卡功能操作详解

    机房管理中HP电脑的增霸卡功能操作详解 一.软件去除保护 1).电脑开机后等待进入增霸卡选择系统界面: 2).按F1帮助,F10进入增霸卡BIOS界面: 3).光标切换到>>>系统还 ...

  4. bzoj——2127: happiness

    2127: happiness Time Limit: 51 Sec  Memory Limit: 259 MBSubmit: 2570  Solved: 1242[Submit][Status][D ...

  5. Microsoft Office Access

    Microsoft Office Access各版本下载地址:http://www.accessoft.com/download.html 简介 access(微软发布的关联式数据库管理系统)一般指M ...

  6. jQuery学习总结1

    一.下载集CDN引入 1.1.官方下载 地址:http://jQuery.com/download/ jq自2.0版本开始,不再支持IE9一下浏览器:自3.0版本开始,针对移动端做了优化处理: 引入 ...

  7. QT学习笔记4:QT中GraphicsView编程

    一.QGraphicsScene 1.QGraphicsScene QGraphicsScene继承自QObject,是一个管理图元的容器,与QGraphicsView合用可以在2D屏幕上显示如线.三 ...

  8. 理解 JavaScript 中的 Function.prototype.bind

    函数绑定(Function binding)很有可能是你在开始使用JavaScript时最少关注的一点,但是当你意识到你需要一个解决方案来解决如何在另一个函数中保持this上下文的时候,你真正需要的其 ...

  9. c++ 常见网络协议头

    //NTP协议 typedef struct _NTP_HEADER { uint8_t _flags;//Flags 0xdb uint8_t _pcs;//Peer Clock Stratum u ...

  10. Unity3D中的UnitySendMessage方法的使用

    UnitySendMessage(“string”,“string”, ***),这是方法,我们至少需要传入两个参数,第一个参数为unity中的一个gameobject名称,第二个参数为这个gameo ...