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.

给出两个二叉树,写一个方法判断两个二叉树是否相等,

如果两个二叉树相等,说明结构相同并且每个节点的值相同。

如果两个节点都为空,则说明相同,返回true,

判断两个根节点的值不同,或者一个为空一个不为空,说明两个树不相同,返回false。

然后再递归左右节点,如果有一个为false或者两个都为false,返回false。

时间O(N), 空间O(h)。

/**
* 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) {
if (p == null && q == null) {
return true;
}
if (p == null || q == null) {
return false;
}
if (p.val != q.val) {
return false;
}
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}

leetcode 100. Same Tree的更多相关文章

  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. LeetCode 100. Same Tree (相同的树)

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

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

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

  5. leetcode 100 Same Tree ----- java

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

  6. Java [Leetcode 100]Same Tree

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

  7. [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 ...

  8. (二叉树 递归 DFS) leetcode 100. Same Tree

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

  9. [LeetCode] 100. Same Tree ☆(两个二叉树是否相同)

    描述 解析 根与根比较,左右子树互相递归比较即可. 代码 /** * Definition for a binary tree node. * public class TreeNode { * in ...

随机推荐

  1. Linux服务器间文件传输

    利用scp传输文件 1.从服务器下载文件 scp username@servername:/path/filename /tmp/local_destination 例如scp codinglog@1 ...

  2. Wget下载终极用法和15个详细的例子

    Wget下载终极用法和15个详细的例子 备注:wget  不支持https 下载,也没有相关https参数,当下载https的时候或以改用 axelWget是一种很好用的因特网下载工具,他具有的很多特 ...

  3. UIView UIwindow

    UI:用户界面,用户能看到的各种各样的页面元素 UIview :代表屏幕上的一个矩形区域,管理界面上的内容       创建UIview 1.开辟空间并初始化视图(初始化时,给出视图位置和大小 2.对 ...

  4. Envelope Letter

    http://www.thefullwiki.org/More_C%2B%2B_Idioms/Envelope_Letter http://www.smallmemory.com/almanac/Co ...

  5. 在 Mac OS X 终端里使用 Solarized 配色方案

    MacOS X 终端solarized配色 相信长期浸泡在终端和代码的小伙伴们都有一套自己喜爱的配色方案.以前一直在用简单.适合阅读的 Terminal.app 配色方案,换到 MacBook Pro ...

  6. (HBase) Error: JAVA_HOME is not set and Java could not be found

    请查看Linux系统环境变量 JAVA_HOME 是否设置.. 两种方法 env //查看全部系统变量 或者 echo $JAVA_HOME //查看JAVA_HOME变量值 如果没有设置则输出空 没 ...

  7. PHP数组处理函数的使用array_map(三)

    /*函数array_map()函数:多数组回调函数---将回调函数作用到给定数组的单元上 * 1.语法:array array_map ( callback callback, array arr1 ...

  8. php-fpm进程关闭与重启脚本详解(转)

    先来理解一下什么是php-fpm PHP-FPM是一个PHP FastCGI管理器,是只用于PHP的. PHP-FPM其实是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中.必须将 ...

  9. weblogic 的安装和配置

    一.安装 1.1安装weblogic8.1 首先从www.bea.com上下载安装文件platform816_linux32.bin,然后在安装文件所在目录下键入 ./platform816_linu ...

  10. [Angularjs]常见api函数

    写在前面 在angularjs中提供了一些常用的函数,比如angular.lowercase(),angular.uppercase(),angular.isString(),angular.isNu ...