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. GMap.NET使用一

    https://greatmaps.codeplex.com/releases/view/20235 从上面网站下载需要的组件dll,也可以下载源码研究,解压后有两个文件夹,如图1所示,根据不同的fr ...

  2. BZOJ2286: [Sdoi2011]消耗战

    建出虚树dp. 把询问点按dfs序排序,用一个以dfs序为关键字的单调栈(以深度为关键字也是一样的),每次将一个询问点与栈顶的点的lca入栈,再将这个询问点入栈,在这个过程中建出一棵树就是虚树.具体看 ...

  3. 把ISO文件加载到虚拟光驱

    1. 下载迅雷 2.  介绍一个下载微软产品的网站(I tell you),里面的下载程序都是破解好的罗.http://www.itellyou.cn/ 3.  在"I tell you&q ...

  4. SQL 字段保留下划线后部分

    select  SUBSTRING(b.SUMMARY,0,charindex('_',b.SUMMARY))as SUMMARY  from UltimusDB.dbo.INCIDENTS b

  5. 4个mysql客户端工具的比较

    mysql是我以前学习和练习所使用的数据,现在在工作中也在使用,之前公司里用oracle,我在做自己的东西的时候觉得用oracle太不方便,于是就找了mysql(当时也考虑过sqlserver,觉得还 ...

  6. vbox 网络配置文件

    sz /etc/sysconfig/network-scripts/ifcfg-eth1 #VAGRANT-BEGIN # The contents below are automatically g ...

  7. easyUI数据表格datagrid之分页

    一.分页函数 /**========================================= * 分页函数 */function pagerFilter(data) { if(typeof ...

  8. C#--之文件操作

    1.从文本文件中读取一行文本 StreamReader sr = new StreamReader("C:\\1.txt"); string readline = sr.ReadL ...

  9. HTML5 Web Form 新增属性和表单验证

    <form>标签的基本属性 method属性:指定浏览器向服务器传送数据的方式,可选: action属性:设置服务器接受和处理表单数据的URL: enctype属性:制定表单数据在发送到服 ...

  10. MySQL索引之前缀索引和索引选择性

    有时需要索引很长的字符列,它会使索引变大而且变慢.一个策略就是模拟哈希索引.但是有时这也不够好,那? 通常可以索引开始的几个字符,而不是全部值,以节约空间并得到好的性能.这使索引需要的空间变小,但是也 ...