100. Same Tree
【题目】
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的更多相关文章
- 100. Same Tree(C++)
100. Same Tree Given two binary trees, write a function to check if they are equal or not. Two binar ...
- 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; ...
- 100.Same Tree(E)
100. same tree 100. Same Tree Given two binary trees, write a function to check if they are the same ...
- <LeetCode OJ> 100. Same Tree
100. Same Tree Total Accepted: 100129 Total Submissions: 236623 Difficulty: Easy Given two binary tr ...
- 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 ...
- leetcode 100. Same Tree、101. Symmetric Tree
100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- LeetCode之100. Same Tree
------------------------------------------ 递归比较即可 AC代码: /** * Definition for a binary tree node. * p ...
- leetcode 100. Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
随机推荐
- JavaScript parseInt() 函数
定义和用法 parseInt() 函数可解析一个字符串,并返回一个整数. 语法 parseInt(string, radix) 参数 描述 string 必需.要被解析的字符串. radix 可选.表 ...
- [地图SkyLine二次开发]框架(1)
项目介绍: 项目是三维地理信息系统的开发,框架MVC4.0 + EF5.0 + Extjs4.2 + SkyLine + Arcgis,是对SkyLine的二次开发. 项目快结束了,先给大家看一眼效果 ...
- nginx 支持laravel 5.3配置
server { listen ; server_name www.baidu.com.cn; root /data/cehuiren/public; #charset koi8-r; #access ...
- springmvc 用注解方式添加事务不生效解决方法
springmvc 事务注册有很多种方法,在此我只mark 用注解方式添加transaction不生效的解决办法. springmvc 注解方法添加事务步骤: 1.在 spring的 root-con ...
- PHP Fatal Error: call to undefined function mysql_connect() [duplicate]
You shouldn't use mysql_* functions to start with. They are deprecated as of PHP 5.5. Use mysqli or ...
- DPDK编译步骤
大页内存分配: NUMA系统(现在的linux一般都是) echo 1024 > /sys/devices/system/node/node0/hugepages/hugepages-2048 ...
- Bible
001 Love your neighbor as yourself. 要爱人如己.--<旧·利>19:18 002 Resentment kills a foo ...
- Spring 相关jar包详细介绍
文章转自:http://blog.csdn.net/farawayhome/article/details/6623946 aspectj目录下是在Spring框架下使用aspectj的源代码和测试程 ...
- Windows 10系统更换Windows 7系统磁盘分区注意事项一
新买的电脑预装系统是WIN10,考虑到兼容性问题,打算更换为WIN7,但在新机上不能直接装WIN7系统,需要在BIOS启动中做一点小改动. 原因分析:由于Windows 8采用的是UEFI引导和GPT ...
- 在Mac上安装Sublime Text3的插件
首先安装插件管理器Package Control 打开Sublime, 按下快捷键 ctrl+', 然后粘贴下面的代码,然后按回车键: import urllib.request,os; pf = ' ...