Lintcode469-Same Tree-Easy
469. Same 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.
Example
Example 1:
Input:{1,2,2,4},{1,2,2,4}
Output:true
Explanation:
1 1
/ \ / \
2 2 and 2 2
/ /
4 4
are identical.
Example 2:
Input:{1,2,3,4},{1,2,3,#,4}
Output:false
Explanation:
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: the root of binary tree a.
* @param b: the root of binary tree b.
* @return: true if they are identical, or false.
*/
public boolean isIdentical(TreeNode a, TreeNode b) {
if (a == null && b == null) {
return true;
}
else if (a != null && b !=null) {
return a.val == b.val
&& isIdentical(a.left, b.left)
&& isIdentical(a.right, b.right);
}
else {
return false;
}
}
}
Lintcode469-Same Tree-Easy的更多相关文章
- UVA12569-Planning mobile robot on Tree (EASY Version)(BFS+状态压缩)
Problem UVA12569-Planning mobile robot on Tree (EASY Version) Accept:138 Submit:686 Time Limit: 300 ...
- Leetcode之101. Symmetric Tree Easy
Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...
- 【leetcode】Minimum Depth of Binary Tree (easy)
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- 【leetcode】Convert Sorted Array to Binary Search Tree (easy)
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 有序 ...
- Leetcode 4.28 Tree Easy
1. 101. Symmetric Tree 用递归. class Solution { public boolean isSymmetric(TreeNode root) { if( root == ...
- Leetcode 226. Invert Binary Tree(easy)
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
- Leetcode 101. Symmetric Tree(easy)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- Leetcode--572. Subtree of Another Tree(easy)
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and no ...
- 93. Balanced Binary Tree [easy]
Description Given a binary tree, determine if it is height-balanced. For this problem, a height-bala ...
- LeetCode: 669 Trim a Binary Search Tree(easy)
题目: Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so th ...
随机推荐
- (转)JVM中的OopMap(zz)
原文地址: http://www.cnblogs.com/strinkbug/p/6376525.html 在读周智明的深入理解JVM虚拟机时,关于枚举根节点/安全点这部分感觉书上写的不是太明白,找了 ...
- @staticmethod和classmethod
之前一直搞不清楚这两个类方法有什么区别,今天着重学习了一下 @staticmethod是静态方法,不需要表示自身对象的self和自身类的cls参数,就跟使用函数一样. class C(object): ...
- SQLSERVER CTE表 row_number()字段 BUG
当CTE中 引用的子视图的字段用了 row_number() over等那么有可能造成row_number() 的对应关系出错的问题 所以CTE row_number() over 排序字段 必须 ...
- 使用hashlib进行文件校验
import hashlib import os path = r'D:\CentOS 64 位' def file_md5(path): """ 文件校验 :param ...
- python中的装包与拆包
python中的装包与拆包 *args和 **kwargs是在python的代码中经常用到的两个参数,初学者对这两个参数的理解可能仅仅限于*args是用于接收多余的未命名参数,**kwargs用于接收 ...
- ionic 需要注意的知识点
- SystemParametersInfo调置壁纸、屏幕保护程序
应用SystemParametersInfo函数可以获取和设置数量众多的windows系统参数.这个小程序就是运用了SystemParametersInfo函数来设置桌面的墙纸,而且程序可以让我们选择 ...
- Android -- 贝塞尔曲线公式的推导和简单使用
1,最近看了几个不错的自定义view,发现里面都会涉及到贝塞尔曲线知识,深刻的了解到贝塞尔曲线是进阶自定义view的一座大山,so,今天先和大家来了解了解. 2,贝塞尔曲线作用十分广泛,简单举几个的栗 ...
- [Android] macOS的Android Studio快捷键
- 快速输入serialVersionUID - - 设置,左上角,Android Studio -> Preferences -> 搜索“Serializable class witho ...
- Jumpserver(跳板机、堡垒机)启动jms Django连接mysql数据库报错
解决办法 根据报错信息 , 去查看官方手册 在settings.py文件夹加入DATABASES['OPTIONS']['init_command'] = "SET sql_mode='ST ...