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中的防篡改对象
由于JavaScript共享的特性,任何对象都可以被放在同一环境下运行的代码修改. 例如: var person = {name:"caibin'} person.age = 21; 即使第 ...
- 特许金融分析师 (CFA) 持证人现在一般在做什么工作?职业分布是怎样的?
特许金融分析师 (CFA) 持证人现在一般在做什么工作?职业分布是怎样的? 陈雨桐 1. 全球范围: 根据 CFA 协会 2014 年 6 月的报告: CFA Institute has over ...
- replace和replaceAll
replace():不可以正则 replaceAll()参数十一正则 replaceFirst()参数是一个正则,匹配第一次出现的 package entity; public class Test2 ...
- js判断当前设备
最近用bootstrap做自适应,发现仍然很难很好的兼容web端和PC端的现实. 仔细观察百度,淘宝,京东等大型网站,发现这些网站都有对应不同客户端的子站. 例如: 站点 PC端url web端url ...
- Linux提权基础
英文原文: Basic Linux Privilege Escalation 在开始之前,我想指出 - 我不是专家. 据我所知,在这个巨大的领域没有一个“魔法”的答案. 这只是我的发现,写出来,共享而 ...
- Dev Express 动态生成XRTable使用总结
1. XRTableCell常见属性 XRTableCell xrTableCell = new XRTableCell(); A. 字体及字体大小 xrTableCell.Font = new S ...
- React-redux-webpack项目总结之用到的Es6基本语法
地址:http://blog.csdn.net/lsgqjh/article/details/53454627 http://www.cnblogs.com/hujunzheng/p/6133648. ...
- mysql-5.7.9安装
版本:mysql-5.7.9-linux-glibc2.5-x86_64.tar.gz(编译版本) 解压: tar -zxvf mysql-5.7.9-linux-glibc2.5-x86_64.ta ...
- git 使用 总结
比较好的教程 http://backlogtool.com/git-guide/cn/intro/intro1_2.html 1.git流程图 2.git 新建仓库 git init git clon ...
- virtualbox虚拟机中的centos与macos共享文件夹
开发中需要用到linux环境,所以使用共享模式开发.通过samba服务器来实现. 环境: 虚拟机 virtualbox 虚拟系统 centos 6.6 本机 macos 192.168.1.102 ...