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中的日期格式转化
package lianxi; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util ...
- eclipse中的快捷键的使用
- Android adb调试
1.首先是adb 修改 devices/amlogic/p201_iptv/system.prop service.adb.tcp.port=5555 或者直接在盒子串口下修改 system/buil ...
- Codeforces 1089K - King Kog's Reception - [线段树][2018-2019 ICPC, NEERC, Northern Eurasia Finals Problem K]
题目链接:https://codeforces.com/contest/1089/problem/K time limit per test: 2 seconds memory limit per t ...
- day5_函数返回值
每个函数都有返回值,如果没有在函数里面指定返回值的话,在python里面函数执行完之后,默认会返回一个None,函数也可以有多个返回值,如果有多个返回值的话,会把返回值都放到一个元组中,返回的是一个元 ...
- Spark入门到精通--(第二节)Scala编程详解基础语法
Scala是什么? Scala是以实现scaleable language为初衷设计出来的一门语言.官方中,称它是object-oriented language和functional languag ...
- IE、360浏览器select属性onchange遇到的坑
在网页头部定义js代码 <script type="text/javascript"> function gradeChange(e){ var selectId = ...
- Golang--选择、循环语法总结
1.判断语句if 条件表达式没有括号 支持初始化表达式 初始化语句的变量自在本block内有效 if a,b,c := 1,2,3;a+b+c>6 { fmt.Println("hah ...
- 七、UIViewController导航栏
概述 上一节我们算是跟UIViewController打了个招呼,同时也表示我们正式介入iOS开发.本节我们将介绍UI界面的一个常用元素:导航栏. iOS为UIViewController提供了内置导 ...
- 8 . IO类-标准IO、文件IO、stringIO
8.1 IO类 #include <iostream> //标准IO头文件 8.2 文件输入输出流 #include <fstream> //读写文件头文件 std::fst ...