[Leetcode 100]判断二叉树相同 Same Tree
【题目】
判断二叉树是否相同。
【思路】
check函数。
p==null并且q==null,返回true;(两边完全匹配)
p==null或q==null,返回false;(p、q其中一方更短)
p.val==q.val,值相同,继续迭代向左向右遍历check(p.left,q.left)&&check(p.right,q.right);
【代码】
public boolean check(TreeNode p, TreeNode q){
if(p==null&&q==null)
return true;
if(p==null||q==null)
return false;
if(p.val==q.val)
return check(p.left,q.left)&&check(p.right,q.right);
return false;
}
[Leetcode 100]判断二叉树相同 Same Tree的更多相关文章
- [Leetcode 101]判断对称树 Symmetric Tree
[题目] Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...
- LeetCode 100. 相同的树(Same Tree) 2
100. 相同的树 100. Same Tree 题目描述 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 每日一算法2019/5 ...
- LeetCode 101. 对称二叉树(Symmetric Tree)
题目描述 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null, ...
- leetcode 98,判断二叉树为BST
方法一,记录子树的上界和下界,root的左子树一定小于root的值,root的右子树一定大于root的值,然后递归左子树和右子树 public class Solution { public bool ...
- 递归 - Leetcode 110 判断二叉树是否为平衡二叉树
110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【easy】110. Balanced Binary Tree判断二叉树是否平衡
判断二叉树是否平衡 a height-balanced binary tree is defined as a binary tree in which the depth of the two su ...
- [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [LeetCode] 366. Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
随机推荐
- css趣味案例:画三角形
代码: <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&q ...
- Win10 | Mac 在server上统一办公
一个非常实际的问题,通常我们主要有三个工作的地点:1,server,用于大型数据的分析和处理:2,办公室的电脑,正式办公:3.自己的电脑,偶尔加班. 不同的工作平台之间很难同步,导致我们的工作和思维分 ...
- Python自学:第二章 合并(拼接字符串)
first_name = "ada" last_name = "lovelace" full_name = first_name + " " ...
- Mycat水平拆分之十种分片规则
水平切分分片实现 配置schema.xml 在同一个mysql数据库中,创建了三个数据库 testdb1,testdb2,testdb3.并在每个库中都创建了user表 <?xml ...
- 20170920xlVBA_FTP_UpDownLoad_DownLoad
'建立应用环境进程 Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpen ...
- 如何理解以太坊ABI - 应用程序二进制接口
很多同学不是很明白以太坊ABI是什么,他的作用是什么,读完本文就明白了. 写在前面 阅读本文前,你应该对以太坊.智能合约有所了解, 如果你还不了解,建议你先看以太坊是什么,也可以观看我们的视频:零基础 ...
- Spring boot @Autowired注解在非Controller中注入为null
参考链接:https://blog.csdn.net/qq_35056292/article/details/78430777
- 『算法设计_伪代码』贪心算法_最短路径Dijkstra算法
Dijkstra算法实际上是一个贪婪算法(Greedy algorithm).因为该算法总是试图优先访问每一步循环中距离起始点最近的下一个结点.Dijkstra算法的过程如下图所示. 初始化 给定图中 ...
- DVWA--XSS解题过程
XSS概念:通常指黑客通过HTML注入纂改了网页,插入恶意脚本,从而在用户浏览网页时,控制用户浏览器的一种攻击. XSS有三种: 反射型xss:只是简单地把用户输入的数据反射给浏览器,简单来说,黑客往 ...
- 第二阶段——个人工作总结DAY05
1.昨天做了什么:将值由一个活动传递到另一个活动. 2.今天打算做什么:打算制作修改密码的界面. 3.遇到的困难:因为是任务是分开的,所需要获取的值是通过另一个活动(不是自己任务)的传递过来的,所以还 ...