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.

思路:同时递归两棵树,如果节点值不相等或者一棵树已经递归到头了而另一棵还没有,返回false;

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p==null&&q==null)
return true; //一开始如果传进两颗空树,返回true
if ((p==null&&q!=null)||(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 -David_Lin的更多相关文章

  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

    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 ----- java

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

  7. Java [Leetcode 100]Same Tree

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

  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. 四、OpenStack—glance组件介绍与安装

    一.glance介绍 Glance是Openstack项目中负责镜像管理的模块,其功能包括虚拟机镜像的查找.注册和检索等. Glance提供Restful API可以查询虚拟机镜像的metadata及 ...

  2. linux centos 用户权限相关总结

    linux上用户管理 以及 相应权限 查看 增加 删除用户 修改密码 用户 用户组 用户默认目录 用户shell路径 等 用户管理 相关文件 1. 查看系统有哪些用户 cat /etc/passwd ...

  3. Ext使用中问题总结

    隐藏 Ext.form.DateField 的触发(trigger)元素使其内容不能修改并使其所有的文本框(text field)显示格式为Y-m-d items : [{ xtype : ' dat ...

  4. golang实现障碍、转弯最少的A*寻路

    目录 目标: 要点: 源码: 目标: 优先寻找无障碍的路径 目标不可达时,寻找障碍最少的路径 路径长度相等时,优先转弯最少的路径 多个目标点时,根据以上要求到达其中一个目标点即可 要点: 最优格子的选 ...

  5. 用OleDb导入Excel时提示驱动错误问题解决办法

    导入格式为xls的excel文件,发生了错误 未处理System.Data.OleDb.OleDbException HResult=-2147467259 Message=外部数据库驱动程序 (1) ...

  6. 马昕璐201771010118 《面对对象程序设计(java)》第九周学习总结

    第一部分:理论知识学习部分 异常:在程序的执行过程中所发生的异常事件,它中断指令的正常执行. Java把程序运行时可能遇到的错误分为两类: 非致命异常:通过某种修正后程序还能继续执行. 致命异常:程序 ...

  7. 31 ArcGIS中后缀一览表(持续更新中……)

  8. 牛人的blog,关于推荐,topic model的

    http://blog.csdn.net/zhoubl668?viewmode=list

  9. Dubbo架构设计及原理详解

    Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合).从服务模型的角度来看,Dubbo采用的是一种非常简单的模 ...

  10. S-CMS企业建站v3几处SQL注入

    0x01 前言 有段时间没有发文章了,主要没挖到比较有意思的漏洞点.然后看最近爆了很多关于S-CMS的漏洞,下载了源码简单挖了一下然后给大家分享一下. 0x02 目录 Wap_index.php sq ...