Identical Binary Tree
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.
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的更多相关文章
- [LintCode] Identical Binary Tree 相同二叉树
Check if two binary trees are identical. Identical means the two binary trees have the same structur ...
- LintCode: Identical Binary Tree
C++ /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; ...
- Lintcode470-Tweaked Identical Binary Tree-Easy
470. Tweaked Identical Binary Tree Check two given binary trees are identical or not. Assuming any n ...
- 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 ...
- [算法专题] Binary Tree
1 Same Tree https://leetcode.com/problems/same-tree/ Given two binary trees, write a function to che ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- 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 ...
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
随机推荐
- 第二次spring冲刺第1天
今天,我们开会讨论了,觉得现阶段的四则运算还不够完善,功能也过于简单,没有太多能吸引人的地方,因次提出以下几点作为后续的修改: 1.计时器,如果具有计时功能,那么就可以增加趣味性. 2.页面跳转,现阶 ...
- CMake系列之一:概念
不同的make工具遵循不同的规范和标准,因此针对不同的标准需要不同的Makefile文件.CMake利用一种平台无关的CMakeList.txt文件定制编译流程,根据目标用户的平台生成本地化的Make ...
- nil Nil NULL NSNull 之间的区别
nil -> Null-pointer to objective- c objectNIL -> Null-pointer to objective- c class 表示对类进行赋空值 ...
- week5-Link Layer
Technology:Internets and Packets course Layer 1 : Link Introduction/The Link Layer moving from histo ...
- TCP/IP之蓟辽督师 转
原创: 刘欣 码农翻身 2016-11-07 本文续<TCP/IP之大明内阁>, 不了解背景的同学可以先看看上一篇文章, 当然这篇也是<TCP/IP之大明邮差>的前传, 主要讲 ...
- Hbase之IP变更后无法启动问题解决
# 修改hbase IP配置文件地址:/opt/hbase-1.1.13/conf/hbase-site.xml <property> <name>hbase.zookeepe ...
- Git push -u orign master 提示hint: not have locally. This is usually caused by another repository push
一.情景 1.在GitHub上创建一个仓库A,并且初始化了readme.md这个文档. 2.在本地用Git Bash初始化仓库A(一开始没有从GitHub上拉下来). git init /* 初始化一 ...
- Asp.Net Mvc的几个小问题
突然想到一些小问题,对写代码影响不大,当是又很实用. MVC 中视图中的model的大小写问题,什么时候用大写,什么时候用小写? 所谓强类型视图,就是通过@model指令指明当前Model(属性)的具 ...
- Python之Numpy数组拼接,组合,连接
转自:https://www.douban.com/note/518335786/?type=like ============改变数组的维度==================已知reshape函数 ...
- MT【172】内外圆
$P,Q$是两个定点,M为平面内一个动点,且$\dfrac{|MP|}{|MQ|}=\lambda(\lambda>0,\lambda\ne1)$, 点M的轨迹围成的区域面积为S , 设$S=f ...