[Leetcode]100. Same Tree -David_Lin
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
思路:同时递归两棵树,如果节点值不相等或者一棵树已经递归到头了而另一棵还没有,返回false;
/**
* 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; //一开始如果传进两颗空树,返回true
if ((p==null&&q!=null)||(p!=null&&q==null))
return false; //递归过程中,一棵树递归到头了,而另一颗没有
if (p.val!=q.val)
return false;
return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
// 同时递归
}
}
[Leetcode]100. Same Tree -David_Lin的更多相关文章
- 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 ...
- 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 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 ----- java
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- Java [Leetcode 100]Same Tree
题目描述: Given two binary trees, write a function to check if they are equal or not. Two binary trees a ...
- (二叉树 递归 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 ☆(两个二叉树是否相同)
描述 解析 根与根比较,左右子树互相递归比较即可. 代码 /** * Definition for a binary tree node. * public class TreeNode { * in ...
随机推荐
- 跟大家谈一谈:涛舅舅家的微信域名检测api的心路历程
微信域名检测,这是近一年来兴起来的一种网络服务,可以通过api接口来对域名进行批量检测,以确认该域名有没有被微信拦截(见红),然后通过编程来实现域名切换保障链接可以正常打开. 涛舅舅工作室从事微信域名 ...
- if(){}使用
1.当作 if else时使用 是判断if()括号内的内容和给定内容是不是相同 package cn.lyun.thread; class Demo{ boolean flag = false; pu ...
- git commit时暂时忽略已提交的文件
当正在修改某文件A,此时需要commit,但是A没修改完暂时不能一起commit. 执行: git update-index --assume-unchanged A的路径 git暂时会忽略该文件的修 ...
- python用类实现xrange
class xrange(object): def __init__(self, start, end=0, step=1): self.start = start self.end = end se ...
- JQuery实现旋转轮播图
css部分 <style> *{ margin:; padding:; } .container{ width:100%; height:353px; margin-top: 20px; ...
- 【自动化测试】robotframework中一些建议可能需要掌握的关键字
这是2019年的第一篇文章,因公司事情较多,导致更新缓慢.这次主要推荐一些可能在使用rf工具的时候需要掌握的关键字. 1. @{cloose_ele} get webelements xpath= ...
- 如何查找MySQL中查询慢的SQL语句(转载)
转载自https://www.cnblogs.com/qmfsun/p/4844472.html 如何在mysql查找效率慢的SQL语句呢?这可能是困然很多人的一个问题,MySQL通过慢查询日志定位那 ...
- 五、JAVA反射、线程
第五节:Java反射.线程 线程 1.进程:进程是程序的基本执行实体,进程是线程的容器. 线程:被称为轻量进程,是程序执行流的最小单元.线程是进程中的一个实 ...
- C语言实型常量
实型常量又称实数或浮点数.在C语言中可以用两种形式来表示一个实型常量. 一.小数形式 小数形式的实型常量由两部分组成:数字和小数点.如:0.12.12...12都是合法的实型常量. 二.指数形式 在C ...
- Centos7 通配符HTTPS证书申请 实测 笔记
环境: 免费通配符HTTPS证书网址: https://letsencrypt.org/ 1.下载证书申请工具 [root@centos ~]# mkdir /opt/letsencrypt -p [ ...