【题目】

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. 修改 phpmyadmin 创建数据库默认编码

    phpmyadmin 创建数据库的默认编码是:latin1_swedish_ci 修改默认编码的方法是 1.登录phpmyadmin 2.在导航部分点:变量 3.Ctrl+F 查找:latin1_sw ...

  2. 高效的插入子节点DocumentFragment

    DocumentFragment 对象 DocumentFragment 接口表示文档的一部分(或一段).更确切地说,它表示一个或多个邻接的 Document 节点和它们的所有子孙节点. Docume ...

  3. iOS开发 iOS10兼容访问http

    添加NSAppTransportSecurity的字典会自动变成  AppTransportSecurity再添加 allow Arbitary Loads  Boolean   YES

  4. 编译2.4.X apache 常见错误

    安装高版本的 apr    apr-util ./configure prefix=/usr/local/apr   ./configure prefix=/usr/local/apr-util -- ...

  5. 【网络】IP地址格式转换(htonl、ntohl;inet_addr、inet_ntoa)

    1.htonl ()和ntohl( ) u_long PASCAL FAR ntohl (u_long netlong); u_short PASCAL FAR ntohs (u_short nets ...

  6. zookeeper 应用

    一,启动一个zk客户端 zookeeper/bin/zkCli.sh ls /  查看根目录下所有节点 create /znode data  创建节点znode,并添加数据内容为data ls /z ...

  7. css学习笔记 3

    css选择符: 通配选择符:* 类选择符:.className 标签选择符 后代选择符:例:p strong ,选择的是p标签内的所有strong标签. 子选择符:> ,只选择父标签内的直接子标 ...

  8. c#数据绑定(4)——向查询中添加参数

    本实例主要练习了ADO.Net 连接到外部数据库的基础上,向查询中添加参数.使用的是ACCESS数据库. 在ACCESS数据库中可以用MSSQL的形式定义操作字符串,也可以采用OLEDB的形式. MS ...

  9. SQL SERVER 2005 DBCC PAGE命令说明

    夏日福利: 小泽玛利亚的“专业摄影”性感写真集:http://947kan.com/video/player-52475-0-0.html ------------------------------ ...

  10. Webstorm 下的Angular2.0开发之路

    人一旦上了年纪,记忆力就变得越来越不好. 最近写了许多的博文,倒不是为了给谁看,而是方便自己来搜索,不然一下子又忘记了. 如果恰巧帮助到了你,也是我的荣幸~~~~~~~~~~~~ 废话不多说,看正题~ ...