[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 ...
随机推荐
- python中装饰器
在介绍装饰器之前,要先了解装饰器的相关基础知识. 嵌套函数: 最后引入一个基本的装饰器的例子: __author__ = "YanFeixu" import time def ti ...
- springboot---->javax.servlet.ServletException
springboot访问静态资源时发生以下异常: javax.servlet.ServletException: Circular view path [login]: would dispatch ...
- 动态改变Listview的item背景颜色和item中字体的颜色
https://blog.csdn.net/qq_14813933/article/details/50417859
- xlrd 安装步骤
官网 https://pypi.python.org/pypi/xlrd 下载 解压 执行python setup.py install进行安装 --------------------------- ...
- python 小练习2
给你一个整数列表L,判断L中是否存在相同的数字, 若存在,输出YES,否则输出NO.解1l=[]for i in L: if L.count(i) != 1: print('YES ...
- Cassandra标准列和超级列
列(column)是Cassandra数据模型中的最基本的数据结构单元.列是一个由列名(key).值(value).时间戳(timestamp)构成的三元组.在关系型数据库中,你需要先定义列的名称和和 ...
- MySQL5.6复制技术(4)-MySQL主从复制过滤参数
复制的过滤主要有2种方式: 在主服务器在把事件从进二制日志中过滤掉,相关的参数是:binlog_do_db和binlog_ignore_db. 在从服务器上把事件从中继日志中过滤掉,相关的参数是re ...
- python模块安装注意事项
在安装python的第三方模块时,需要注意路径问题. 1.如果python是按默认位置安装的,则可以直接在命令提示符中进行安装,即pip install module_name. 2.如果python ...
- html 巧用data-for藏自定义属性
<div class="form-ele"> <label for="week" class="label col-1"& ...
- 读取图片列表——CNN输入
image_list = [] new_file_list = [] for root, _, file_list in os.walk(predict_dir): new_file_list += ...