[LeetCode] 100. Same Tree ☆(两个二叉树是否相同)
描述
解析
根与根比较,左右子树互相递归比较即可。
代码
/**
* 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) {
return false;
} else if (p.val == q.val) {
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
} else {
return false;
}
}
}
[LeetCode] 100. Same Tree ☆(两个二叉树是否相同)的更多相关文章
- [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- (二叉树 递归 DFS) leetcode 100. Same Tree
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- LeetCode 100. Same Tree (相同的树)
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- leetcode 100. Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- LeetCode 100. Same Tree相同的树 (C++)
题目: Given two binary trees, write a function to check if they are the same or not. Two binary trees ...
- LeetCode:94_Binary Tree Inorder Traversal | 二叉树中序遍历 | Medium
题目:Binary Tree Inorder Traversal 二叉树的中序遍历,和前序.中序一样的处理方式,代码见下: struct TreeNode { int val; TreeNode* l ...
- LeetCode:144_Binary Tree Preorder Traversal | 二叉树的前序遍历 | Medium
题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: struct TreeNode { int val; TreeNode* left; ...
- LeetCode:145_Binary Tree Postorder Traversal | 二叉树后序遍历 | Hard
题目:Binary Tree Postorder Traversal 二叉树的后序遍历,题目要求是采用非递归的方式,这个在上数据结构的课时已经很清楚了,二叉树的非递归遍历不管采用何种方式,都需要用到栈 ...
- 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 ...
随机推荐
- mutect/mutsig/gistic官网汇总
http://software.broadinstitute.org/software/cprg/
- 【Java】【集合】
[1. ]HashMap,LinkedHashMap,TreeMap对比 共同点: HashMap,LinkedHashMap,TreeMap都属于Map:Map 主要用于存储键(key)值(valu ...
- Mysql 函数使用记录(三)——UNIX_TIMESTAMP() 、UNIX_TIMESTAMP(date)
参考资料:https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_unix-timestamp UN ...
- Add Two Numbers ,使用链表参数
# Definition for singly-linked list. class ListNode(object): def __init__(self, x): self.val = x sel ...
- [转]QT中QString与string的转化,解决中文乱码问题
QString str2qstr(const string str) { return QString::fromLocal8Bit(str.data()); } string qstr2str(co ...
- 《剑指offer》第五十八题(左旋转字符串)
// 面试题58(二):左旋转字符串 // 题目:字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部. // 请定义一个函数实现字符串左旋转操作的功能.比如输入字符串"abcde ...
- Codeforces 1038 E - Maximum Matching
E - Maximum Matching 思路: 欧拉图 定理:一个度数为奇数的点的个数小于等于2的联通图存在欧拉回路 对于这道题目的图,点的个数为4,所以最坏的情况下4个点的度数都为奇数,在这种情况 ...
- Codeforces 36B - Fractal
36B - Fractal 思路:分形 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #def ...
- Eclipse项目左侧调整显示或者不显示路径
View Menu(倒三角)-->Package Presentation-->选择Flat或Hierarchical
- Configuring Groovy SDK within IntelliJ IDEA
一.原因 IntelliJ IDEA期待一个the standard Groovy SDK 二.解决方案: 下载安装Groovy就可以了 官网下载地址: http://groovy-lang ...