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.

判断两棵树是否相同。

很简单。递归。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) { return getResult(p,q);
}
public boolean getResult(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; return getResult(p.left,q.left)&&getResult(p.right,q.right); }
}

leetcode 100 Same Tree ----- java的更多相关文章

  1. LeetCode 100. Same Tree (判断树是否完全相同)

    100. Same Tree Given two binary trees, write a function to check if they are the same or not. Two bi ...

  2. leetcode 100. Same Tree、101. Symmetric Tree

    100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...

  3. Java [Leetcode 100]Same Tree

    题目描述: Given two binary trees, write a function to check if they are equal or not. Two binary trees a ...

  4. Java for LeetCode 100 Same Tree

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  5. LeetCode 100. Same Tree (相同的树)

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  6. [LeetCode] 100. Same Tree 相同树

    Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...

  7. leetcode 100. Same Tree

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  8. leetcode 101 Symmetric Tree ----- java

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  9. [Leetcode]100. Same Tree -David_Lin

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

随机推荐

  1. Android打开新的Activity并同时关闭当前Activity

    Intent it = new Intent(); it.setClass(EditActivity.this, MainActivity.class); it.setFlags(Intent.FLA ...

  2. 黑马程序员——C语言基础语法 关键字 标识符 注释 数据及数据类型

    Java培训.Android培训.iOS培训..Net培训.期待与您交流! (一下内容是对黑马苹果入学视频的个人知识点总结) (一)C语言简单介绍 (1)C语言程序是由函数组成的任何C语言程序都是由一 ...

  3. Struts2 validate校验

    一般的,用户注册的时候,我们需要校验一些用户提交过来的参数. 一般有两道屏障,一是在前台页面上使用js进行验证,直接杜绝了不正常信息的提交.二是将提交过来的信息进行验证,不通过则返回注册页面并显示错误 ...

  4. 记录一些容易忘记的属性 -- UIKeyboard

    //UIKeyboardWillShowNotification这个通知在软键盘弹出时由系统发送    //UIKeyboardWillShowNotification 通知:键盘将要显示的通知    ...

  5. Event Handling on Mac

    Keyboard/Mouse Event + Cocoa AppleEvent + Cocoa AppleEvent + CommandLine App(w/o UI) + CoreFoundatio ...

  6. java.util 集合框架集合

    java的集合框架为程序提供了一种处理对象组的标准方式.设计了一系列标准泛型接口: ⑴Collection ()接口,扩展了Iterable接口,位于集合层次结构的顶部,因此所有的集合都实现Colle ...

  7. PHP面向对象的继承

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 0816 1459 json & pickle ,目录导入,目录规范

    ---恢复内容开始--- 1.json & pickle 磁盘上只能存储字符串或二进制数据,直接存字典.列表.元组等是存不了的,所以需要把各种数据转换成字符串格式,然后再存到硬盘. 直接将一个 ...

  9. Comments

    Nothing can be quite so helpful as a well-placed comment.Nothing can clutter up a module more than f ...

  10. CentOS 6.6 nginx PHP 配置

    /************************************************************************* * CentOS 6.6 nginx PHP 配置 ...