Check if two binary trees are identical. Identical means the two binary trees have the same structure and every identical position has the same value.

Example

    1             1
/ \ / \
2 2 and 2 2
/ /
4 4

are identical.

    1             1
/ \ / \
2 3 and 2 3
/ \
4 4

are not identical.

 /**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param a, b, the root of binary trees.
* @return true if they are identical, or false.
*/
public boolean isIdentical(TreeNode a, TreeNode b) {
if (a == null && b == null) return true;
if (a == null || b == null) return false; if (a.val != b.val) return false; return isIdentical(a.left, b.left) && isIdentical(a.right, b.right);
}
}

Identical Binary Tree的更多相关文章

  1. [LintCode] Identical Binary Tree 相同二叉树

    Check if two binary trees are identical. Identical means the two binary trees have the same structur ...

  2. LintCode: Identical Binary Tree

    C++ /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; ...

  3. Lintcode470-Tweaked Identical Binary Tree-Easy

    470. Tweaked Identical Binary Tree Check two given binary trees are identical or not. Assuming any n ...

  4. LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]

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

  5. [算法专题] Binary Tree

    1 Same Tree https://leetcode.com/problems/same-tree/ Given two binary trees, write a function to che ...

  6. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  7. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  8. Leetcode, construct binary tree from inorder and post order traversal

    Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...

  9. [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

随机推荐

  1. FZU-SE-K 第一次累计得分排行榜

    FZU-SE-K 第一次累计得分排行榜 包含第一.二.三次作业 排行 恭喜 248 文航 同学获得本期小黄衫 原图戳 这里 明细 1 - 第一次作业映射分数 2 - 第二次作业映射分数 3 - 第三次 ...

  2. Alpha冲刺——day7

    Alpha冲刺--day7 作业链接 Alpha冲刺随笔集 github地址 团队成员 031602636 许舒玲(队长) 031602237 吴杰婷 031602220 雷博浩 031602634 ...

  3. Beta冲刺——day4

    Beta冲刺--day4 作业链接 Beta冲刺随笔集 github地址 团队成员 031602636 许舒玲(队长) 031602237 吴杰婷 031602220 雷博浩 031602134 王龙 ...

  4. dos批量导入不受信任的证书及软件限制策略的应用

    certmgr.exe -add "证书.cer" -s -r localMachine Disallowed 导入授信机构 certmgr -add "证书.cer&q ...

  5. CentOS修改主机名字

    目录 查看hostnmae 修改hostname 远程别名/etc/hosts 查看hostnmae [root@centos ~]$ hostname centos 修改hostname [root ...

  6. Delphi中快速修改变量名称

    鼠标左键选择需要修改的范围,左侧会出现一个同步编辑模式(Sync Edit Mode),快捷键:CTRL+SHIFT+J,它可以让我们清楚地编写或改写多个同一参数.

  7. Redis的核心Hystrix在Spring mvc的使用

    核心Hystrix,Hystrix对于接口调用具有很好的保护,能在多服务依赖的分布式系统中,有效的提供应用的可用性,并且对失败应用进行熔断和恢复检查,让应用在复杂的环境中也能各种稳. http://t ...

  8. SSH-keygen用法

    很多朋友在用github管理项目的时候,都是直接使用https url克隆到本地,当然也有有些人使用 SSH url 克隆到本地.然而,为什么绝大多数人会使用https url克隆呢? 这是因为,使用 ...

  9. JSP 获取真实IP地址的代码

    [转载]JSP 获取真实IP地址的代码 JSP 获取真实IP地址的代码 在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.   ...

  10. redis后台启动配置

    在cmd窗口启动redis,窗口关闭后再次操作会报错. 将redis安装为服务,可使其在后台启动,无须担心误操作关闭服务窗口. 配置如下: 进入redis目录,输入如下命令执行即可: redis-se ...