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. Daily Scrum 11.1

    今天放假一天,明天又是新的一周,预计开始Alpha版本所有功能的整合和优化,争取在两天内完成各种功能的整合. Member Task on 11.1 Task on 11.2 仇栋民 放假一天 开始T ...

  2. (Alpha)Let's-NABC

    效果图 NABC Need 在日常生活中我们也许经常有这样的疑问:在空闲时间内经常感到无处可去:抑或是有个心血来潮的想法却无人响应:或者困扰于自己狭小的交际圈,希望能有更多的朋友.(打个羽毛球没人一起 ...

  3. DHCP分配固定IP

    https://www.cnblogs.com/liu1026/p/9829337.html 按照上面的操作实验好后在DHCP服务端的配置文件中加入

  4. XenServer DVSC

    DVSC: Distribute Virtual Swithc Controller(分布式虚拟交换机控制器). DVSC是一种逻辑的集中式的虚拟交换机,它能让你控制来自于不同XenServer Ho ...

  5. 【菜鸟】RESTful 架构详解

    RESTful 架构详解 分类 编程技术 1. 什么是REST REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移. 它首次 ...

  6. contentInsetAdjustmentBehavior各个值之间的区别

    iOS11也出了不少时候了网上说适配的文章一大堆.关于contentInsetAdjustmentBehavior这个参数之间的区别,好像没什么人能说明.往下看的前提是你已经知道什么是安全区域,没看明 ...

  7. php 微信公众号上传永久素材

    <?php namespace Mob\Controller; use Think\Controller; class WxmaterialController extends Controll ...

  8. Java 8新特性之接口改善(八恶人-1)

    Daisy Donergue 多莫歌·黛西 “By woman, you mean her?” 她也能叫女人? Java 8在13年9月发布,写这篇博文的时间已经是17年12月份了.来的有点晚,但是有 ...

  9. 【题解】 [HNOI2015]落忆枫音 (拓扑排序+dp+容斥原理)

    原题戳我 Solution: (部分复制Navi_Aswon博客) 解释博客中的两个小地方: \[\sum_{\left(S是G中y→x的一条路径的点集\right))}\prod_{2≤j≤n,(j ...

  10. 《剑指offer》— JavaScript(29)最小的K个数

    最小的K个数 题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4. 思路一 使用JavaScript的Array对象的so ...