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 ...
随机推荐
- Java编程基础篇第五章
数组概述 概念:数组是存储同一种数据类型多个元素的集合.也可以看成是一个容器.数组既可以存储基本数据类型,也可以存储引用数据类型.应用场景:为了存储同种数据类型的多个值 数组定义格式 格式1:元素类型 ...
- ERP实施顾问--理解客户的解决方案与实际需求
在企业进行信息化时实施方的顾问都会来现场进行"需求调研",再根据"调研"的结果进行双方确认,确认后按此蓝本进行开发实施. 一切看上去都很美好,需求明确.开发顺利 ...
- 清除 System.Web.Caching.Cache 以"xxx"开头的缓存
public static void ClearStartCache(string keyStart) { List<string> cacheKeys = new List<str ...
- ES6 数组方法拓展
ES6 数组方法拓展 1.Array.from() Array.from方法用于将两类对象转为真正的数组:类似数组的对象(array-like object)和可遍历(iterable)的对象(包括E ...
- ios 11越狱移除
https://baijiahao.baidu.com/s?id=1593748369421662278&wfr=spider&for=pc 不越狱抹机步骤: 点击设置 进入自带游览 ...
- day5_非空即真非零即真
非空即真(字符串.元组.列表.字典.None),非零即真(指的是int类型或数字这种) # d={}# l=[]# t=()# a = ''# b = None以上都代表空 举例1: name = i ...
- 数据库join操作(MySQL)
左连接,右连接,内连接 1.原始表:左表user_t,右表user_info 2.执行连接 #LEFT JOIN ; #RIGHT JOIN ; #INNER JOIN ; 2.1左连接:以左边为主 ...
- 将python代码打包成一个app/exe
前言 打包的代码通常都需要写一个简单的界面,一般用PyQt来写.用PyQt写界面的方法请戳这里:PyQt5的安装及基本配置 PyQt5教程 python提供了几个用来打包的模块,主要有py2ap ...
- python框架之Django(14)-rest_framework模块
APIView django原生View post请求 from django.shortcuts import render, HttpResponse from django import vie ...
- Mysql导入表信息[Err] 1067 - Invalid default value for '字段名'
修改mysql配置文件 vi /etc/my.cnf //添加以下配置 sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISI ...