[LC] 100. Same Tree
Given two binary trees, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical and the nodes have the same value.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
} else if (p == null && q != null || p != null && q == null) {
return false;
} else if (p.val != q.val) {
return false;
}
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
[LC] 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 ...
- 100. Same Tree同样的树
[抄题]: Given two binary trees, write a function to check if they are the same or not. Two binary tree ...
- 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 ...
随机推荐
- 2019牛客暑期多校训练营(第七场)A.String【最小表示法】
传送门:https://ac.nowcoder.com/acm/contest/887/A 题意:大意就是给你一个只含有0和1的字符串,找出一种分割方法,使得每个分割出的字符串都是在该字符串自循环节中 ...
- Python笔记_第一篇_面向过程_第一部分_2.内存详解
Python的很多教材中并没有讲内存方面的知识,但是内存的知识非常重要,对于计算机工作原理和方便理解编程语言是非常重要的,尤其是小白,因此需要把这一方面加上,能够更加深入的理解编程语言.这里引用了C语 ...
- python3转义编码
s = 'dy电影' print(s) # dy电影 print(type(s)) # <class 'str'> print(s.encode('utf-8')) # b'dy\xe7\ ...
- 实现Action
实现Action 对于开发者来说,Action才是应用的核心,开发者需要提供大量的Action类,并在Struts.xml文件中配置Action.Action类中包含了用户请求的处理逻辑,Action ...
- UML-SSD总结
1.不是所有场景都需要画SSD.需要画SSD的场景: 1).主成功场景 2).频繁发生的场景 3).复杂的场景 2.角色 1).参与者 2).系统(没有类,即黑盒) 3.画SSD时间不要过长,一般几分 ...
- 个性化bash
zsh/on-my-zsh Ubuntu,deepin, 等可以使用 apt install 的系统 apt install zsh 一般就可以自动安装 RedHat(Fedora,Centos) ...
- NSPredicate 应用
//查询单词里面包含“ang”的字符串 NSArray *array = [[NSArray alloc]initWithObjects:@"beijing",@"sha ...
- LeetCode——71.简化路径
以 Unix 风格给出一个文件的绝对路径,你需要简化它.或者换句话说,将其转换为规范路径. 在 Unix 风格的文件系统中,一个点(.)表示当前目录本身:此外,两个点 (..) 表示将目录切换到上一级 ...
- python学习——tuple
tuple 上次谈到了列表,而这次所谈的元组其实和列表有许多相似的地方,故元组又叫"戴上了枷锁的列表".这是因为元组不能改动内部的元素,所以就不能使用上次谈到的append.ext ...
- 20)PHP,数组的遍历
然后开始使用这2个函数和while循环结构来实现数组遍历: 形式: reset($arr1); while ( list ($key, $value ) = each( $arr1) ) //从数组$ ...