[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 ...
随机推荐
- composer修改中文镜像
composer config -g repo.packagist composer https://packagist.phpcomposer.com
- missing seperator error when [make all]
https://stackoverflow.com/questions/16931770/makefile4-missing-separator-stop makefile has a very st ...
- 【文献04】无人驾驶高速AWID-AWIS车辆运动控制研究
参考:阮久宏, 李贻斌, 荣学文, et al. 无人驾驶高速AWID-AWIS车辆运动控制研究[J]. 农业机械学报, 2009, 40(12):37-42. https://drive.wps.c ...
- HTML第三章总结
在这一章节中,主要讲了 HTML 中众多的 element,element 就像在建筑房屋时候的材料,它可以分为两种: Block Element Inline ElementBlock Elemen ...
- R quantile函数 | cut函数 | sample函数 | all函数 | scale函数 | do.call函数
取出一个数字序列中的百分位数 1. 求某一个百分比 x<-rnorm(200) quantile(x,0.9) 2. 求一系列的百分比 quantile(x,c(0.1,0.9)) quanti ...
- spring ----> 搭建spring+springmvc+mybatis出现的几个问题
环境: idea ce 2018.1+maven3.5.3+mysql8.0.11+jdk1.8 spring4.3.7+spring mvc4.3.7+mybatis3.4.1+tomcat7.0. ...
- WPF程序开发方法小总结
1.先做静态界面(静态界面带有 示例 数据---> 展示布局效果) 2.然后在VM写完之后,再对静态界面绑定数据源属性
- caffe 动态库 Release X64
Release X64平台 createdll.h#ifndef CREARDLL_H_#define CREARDLL_H_ extern "C" _declspec(dllex ...
- function_exists
在已经定义的函数列表(包括系统自带的函数和用户自定义的函数)中查找 function_name. 如果 function_name 存在且的确是一个函数就返回 TRUE ,反之则返回 FALSE .
- 22. Generate Parentheses C++回溯法
把左右括号剩余的次数记录下来,传入回溯函数. 判断是否得到结果的条件就是剩余括号数是否都为零. 注意判断左括号是否剩余时,加上left>0的判断条件!否则会memory limited erro ...