【题目】

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.

【解题】

思路:

这道题要判断树形一样,在这个基础上val一样。

共有5种树型:

1. node为null

2. node是leave

3. node只有左child

4. node只有右child

5. node有两个children

Base cases:

1和2是base case树形

先判断是不是两个node都为null,如果是,return true;

在判断是不是node一个为null一个不为null,如果是,return false;

如果两个node都是leaves, 判断val是不是相等,等则true,不等则false;

Recursive cases:

3, 4和5是recursive case树形

树形均为3: 判断val相等和左child相等

树形均为4: 判断val相等和右child相等

树形均为5: 判断val相等, 左child相等和右child相等

其他:树形不一样

/**
* 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) {
// base case:
// two nodes are both null: true
// one is null and another is not: false
// both are leaves: check the val of the two nodes
if (p == null && q == null) {
return true;
} else if (p == null || q == null) {
return false;
} else if (p.left == null && q.left == null && p.right == null && q.right == null) {
if (p.val == q.val) {
return true;
} else {
return false;
} // recursive case:
} else if (p.left == null && q.left == null && p.right != null && q.right != null) {
return p.val == q.val && isSameTree(p.right, q.right); } else if (p.left != null && q.left != null && p.right == null && q.right == null) {
return p.val == q.val && isSameTree(p.left, q.left); } else if (p.left != null && q.left != null && p.right != null && q.right != null) {
return (p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right)); } else {
return false;
}
}
}

100. Same Tree的更多相关文章

  1. 100. Same Tree(C++)

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

  2. LeetCode Javascript实现 100. Same Tree 171. Excel Sheet Column Number

    100. Same Tree /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; ...

  3. 100.Same Tree(E)

    100. same tree 100. Same Tree Given two binary trees, write a function to check if they are the same ...

  4. <LeetCode OJ> 100. Same Tree

    100. Same Tree Total Accepted: 100129 Total Submissions: 236623 Difficulty: Easy Given two binary tr ...

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

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

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

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. LeetCode之100. Same Tree

    ------------------------------------------ 递归比较即可 AC代码: /** * Definition for a binary tree node. * p ...

  9. leetcode 100. Same Tree

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

随机推荐

  1. F12定义到元数据问题解决

    删除引用中的该dll,重新引用选择解决方案下的项目引用,下次F12就不会进入到元数据而是进入到源代码中方便调试

  2. xib的UIScrollView自适应高度

    1.首先,我们先把这个size classes关了(需要使用的童鞋无视掉,也无视掉我的工程名,这是我弄高德地图创建的工程) 2.添加一个scrollview上去,设置上下左右约束为0 3.然后搞一个v ...

  3. [POJ3111]K Best(分数规划, 二分)

    题目链接:http://poj.org/problem?id=3111 求选k对数,使得上述式子值最大.容易想到设左边为一个值,对式子变形以下,得到sigma(v-r*w))==0的时候就是最大的,& ...

  4. python与C++交互

    python和C++能进行有效的交互,c++调用Python的一些小用法 写了一个python脚本导入发生异常,可能是编码问题(如存在中文),Python默认的是ASCII可加上:#!/usr/bin ...

  5. JAVA 1.1

    1. JASE : J2SE 这个就是我们现在在学的东西,他是一切Java的核心基础 JAME :J2ME : 他是Java的一个微型版,主要用来做移动开发 JAEE :J2EE Java企业版本,主 ...

  6. Android复制粘贴文字

    /** * 实现文本复制功能 * * @param content */ public static void copy(String content, Context context) {// 得到 ...

  7. myeclipse连接oracle步骤

    1.加载ojdbc.jar驱动(路径:E:\myoracle\oracle\product\11.2.0\dbhome_1\jdbc\lib) 2.String url = "jdbc:or ...

  8. PhpStorm配置svn时提示需要证书:authentication required的解决方法,总是弹出

    总是弹出下面的框框,每次输入svn账号密码,还是不行. 于是上网查了半天,需要安装和配置SlikSvn.于是就下载安装了. 完了还是不行,就尝试着配置. 上面和下面的加上,svn.exe的地址. 再次 ...

  9. javascript高级编程3第二章:在html中使用javascript

    2.1 <script>元素 向html页面中插入javascript的主要方法,就是使用<script>元素.这个元素被加入到正式的html规范中.html4.01为< ...

  10. mysql delete 使用别名 语法

    今天删除数据,写了这么条sql语句, DELETE   from  sys_menus s WHERE s.MENU_ID in (86,87,88); 结果报错.. [Err] 1064 - You ...