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

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. JavaScript—文字自动变化为自定义颜色

    效果: JS代码: var ColorTimer; var Colorforn = 0; //颜色代码 var ColorArray = new Array("#00CCCC", ...

  2. jvm的内存模型

    转自:https://www.cnblogs.com/dingyingsi/p/3760447.html 我们知道,计算机CPU和内存的交互是最频繁的,内存是我们的高速缓存区,用户磁盘和CPU的交互, ...

  3. R中绘制聚类的离散图

    R中利用cluster简单的绘制常见聚类离散图 # 引入cluster库(clara.fanny) library(cluster) # 聚类散点图绘制 # 引入factoextra,cluster库 ...

  4. Fundamental theorem of arithmetic 为什么1不是质数

    https://en.wikipedia.org/wiki/Fundamental_theorem_of_arithmetic In number theory, the fundamental th ...

  5. E.F.Codd IBM Oracle

    http://blog.sciencenet.cn/home.php?mod=space&uid=287179&do=blog&id=883429 <传奇>: “宁 ...

  6. 模仿Masonary写一个计算器

    1.CaculatorMaker @interface CaculatorMaker : NSObject @property(nonatomic,assign)int result; -(Cacul ...

  7. tcp/udp/socket 端口映射,转发小工具

    1) 利用 Python 的 Socket 端口转发,用于远程维护 https://github.com/knownsec/rtcp 2) 反向端口转发工具 Reverse TCP Port to U ...

  8. docker——Etcd高可用键值对数据库

    一.简介 Etcd按照官方介绍: Etcd is a distributed, consistent key-value store for shared configuration and serv ...

  9. 解决不能在本地使用JQuery load的方法

    $.ajaxSetup({ xhr: function () { if ("ActiveXObject" in window) { return new ActiveXObject ...

  10. GIT学习笔记(1):创建版本库

    GIT学习笔记(1):创建版本库 创建版本库 1.创建合适目录并初始化为仓库 版本库即需要交由Git进行版本控制的目录,其下所有文件的修改.删除,Git都能跟踪还原. 说明:初始化后,当前目录下会多出 ...