算法分析:这道题很简单,利用递归即可。

public class SameTree
{
public boolean isSameTree(TreeNode p, TreeNode q)
{
if(p == null)
{
return q == null;
}
if(q == null)
{
return p == null;
}
if(p.val == q.val)
{
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
else
{
return false;
}
}
}

Same Tree,判断两个二叉树是不是相同的树,结构相同,每个节点的值相同的更多相关文章

  1. same tree(判断两颗二叉树是否相等)

    Input: 1 1 / \ / \ 2 3 2 3 [1,2,3], [1,2,3] Output: true Example 2: Input: 1 1 / \ 2 2 [1,2], [1,nul ...

  2. LeetCode 100. Same Tree 判断两棵二叉树是否相等 C++

    Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...

  3. 【遍历二叉树】08判断两个二叉树是否相同【Same Tree】

    迭代版本用的是二叉树的DFS,中的root->right->left +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...

  4. Same Tree 比较两个二叉树是否完全相同

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  5. 101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树

    给定一个二叉树,检查它是否是它自己的镜像(即,围绕它的中心对称).例如,这个二叉树 [1,2,2,3,4,4,3] 是对称的.    1   / \  2   2 / \ / \3  4 4  3但是 ...

  6. java实现判断两个二叉树是否相同

    1.定义树节点类:节点值.左节点.右节点.构造器 2.先判断树是否为空的情况 3.树不为空时,判断节点所指的值是否相等,若相等,则递归判断节点的左右节点是否相同,相同则返回true /** * Def ...

  7. LeetCode 101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For ex ...

  8. 判断一棵二叉树是否为AVL树

    思路:AVL树是高度平衡的二叉搜索树,这里为了清晰说明,分别判断是否为搜索树,是否为平衡树. struct TreeNode { struct TreeNode *left; struct TreeN ...

  9. LeetCode 617. Merge Two Binary Tree (合并两个二叉树)

    Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...

随机推荐

  1. java 空间四点定位,可跟据已知的四点坐标(x,y,z)及距离计算所在位置坐标

    public static void main(String args[]) { try{ float point[]=new float[3]; Location loc = new Locatio ...

  2. oracle的cursor

    oracle的cursor 转自:http://www.cnblogs.com/shengtianlong/archive/2010/12/31/1922767.html 1,什么是游标? ①从表中检 ...

  3. js判断浏览器是否安装Flash插件,并提示安装或开启

    var flashChecker = function() { var hasFlash = 0; //是否安装了flash var flashVersion = 0; //flash版本 if(do ...

  4. Oracle卸载之正确卸载rac数据库的方法(MOS卸载方法)

    一.关闭数据库和资源 1.节点1 [root@node1 bin]# pwd /u01/app/11.2.0/grid/bin [root@node1 bin]# ./crsctl stop crs ...

  5. Elasticsearch之settings和mappings(图文详解)

    Elasticsearch之settings和mappings的意义 简单的说,就是 settings是修改分片和副本数的. mappings是修改字段和类型的. 记住,可以用url方式来操作它们,也 ...

  6. sql_q.format

    field_q_insert = 'id, title, number, created,content'sql_q = 'INSERT INTO testquestion ({}) VALUES ( ...

  7. 一致性hash 大众点评订单分库分表实践

    井底之蛙 https://mp.weixin.qq.com/s?src=3&timestamp=1543228894&ver=1&signature=uF6nV0yYseJ55 ...

  8. php array_merge和运算符+

    其实很多时候我都很疑惑为什么同维度的数组不能直接使用运算+直接进行相加,然后结果就是两个数组合并的在一起的新结果,这个就有点跟array_merge合并函数类似了,接下来就来看下这两种合并的方式到底有 ...

  9. iptables综述

    1 概述 如下图所示,iptables共有Filter,Nat,Mangle和RAW共四个table,每个table还有若干个chain,每个chain中还包含若干个rule 1.1 Filter t ...

  10. 【调研与分析】标杆学习、知识管理和竞争情报的关系——From Team

    注: 1)红色实线箭头表示可以直接使用:例如竞争情报业务输出产品之一的标杆文档可以直接作为公司标杆学习中外部标杆的资料: 2)蓝色虚线箭头表示转化后使用或者间接利用的关系:例如专题调研可以帮助建立内部 ...