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 ...
随机推荐
- Java类的加载
1.类的加载步骤 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载.连接.初始化三步来实现对这个类的初始化 加载:将class文件读入内存,并为之创建一个Class对象,任何类被使用 ...
- nginx+lua实现简单的waf网页防火墙功能
原文:http://www.2cto.com/net/201608/534272.html 安装LuaJIT http://luajit.org/download/LuaJIT-2.0.4.tar.g ...
- dom4j的quickstart
我所理解的dom4j就是用来解析XML文档的,XML文档的重要性不言而喻,用过框架的人谁不知道呢,是不是.但是实际上需要我们自己来解析XML文档的应用场景感觉不是很多,毕竟该解析的XML都已经被框架很 ...
- php : 收集整理的非常有用的函数
项目中经常会需要一些让人头疼的函数,作为开发者应该整理一个自己的函数库,在需要之时复制过来即可.以下是收集整理数十个PHP项目中常用的函数 1.PHP加密解密 PHP加密和解密函数可以用来加密一些有用 ...
- hiho #1329 : 平衡树·Splay
#1329 : 平衡树·Splay 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:小Hi,上一次你跟我讲了Treap,我也实现了.但是我遇到了一个关键的问题. ...
- asp.net报错“尝试读取或写入受保护的内存。这通常指示其他内存已损坏”的解决办法
来源:http://ajxfxb.blog.163.com/blog/static/56675086201411634336878/ 作者是:没完没了的工作 asp.net报错“尝试读取或写入受保护的 ...
- Linq常用语法详细
1.简单的linq语法 //1 var ss = from r in db.Am_recProScheme select r; //2 var ss1 = db.Am_recProScheme; // ...
- express+gulp构建项目(四)env环境变量
这里的文件的作用是负责设置env环境变量和日志. index.js try { require('dotenv').load({silent: true}); //dotenv从一个.env文件中读取 ...
- Unity脚本时间执行顺序
1.Awake Awake用于脚本唤醒.此方法为系统执行的第一个方法,用于脚本的初始化,在脚本的生命周期中只执行一次. 2.Start Start方法在Awake之后执行,在脚本生命周期中只执行一次. ...
- css3实现颜色渐变以及兼容性处理
有时我们会看到网站上的一些图片是渐变色的,这些图片有的是ui设计出来的,有的则是直接通过css3制作出来的.下面就讲一下css3实现渐变色的方法,以及在各个浏览器上的兼容性. CSS3 Gradien ...