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.
 
 
注意:
return a.val == b.val... 不要写成return a == b ....!!!
递归法代码:
/**
* 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的更多相关文章

  1. 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 ...

  2. Leetcode之101. Symmetric Tree Easy

    Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...

  3. 【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 ...

  4. 【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. 有序 ...

  5. Leetcode 4.28 Tree Easy

    1. 101. Symmetric Tree 用递归. class Solution { public boolean isSymmetric(TreeNode root) { if( root == ...

  6. 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 ...

  7. Leetcode 101. Symmetric Tree(easy)

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  8. 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 ...

  9. 93. Balanced Binary Tree [easy]

    Description Given a binary tree, determine if it is height-balanced. For this problem, a height-bala ...

  10. 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 ...

随机推荐

  1. P1115 最大子段和

    题目描述 给出一段序列,选出其中连续且非空的一段使得这段和最大. 输入输出格式 输入格式: 第一行是一个正整数NNN,表示了序列的长度. 第二行包含NNN个绝对值不大于100001000010000的 ...

  2. 方差+标准差+四分位数+z-score公式

    一.方差公式 $S^2 = \frac{1}{N}\sum_{i=1}^{N}(X_i - \mu)^2 = \frac{1}{N}[(X_1-\mu)^2 + (X_2-\mu)^2 + ... + ...

  3. react引入方式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. php 中的关系运算符

    php中的关系运算符有:&&(and).||(or).xor(亦或).!(非). 名称 关系 示例 结果 and 与 $x and $y 如果 $x 和 $y 都为 true,则返回 ...

  5. 转载关于Python Web后端开发面试心得

    先介绍下我的情况:通信背景,工作一年多不到两年.之前一直在做C++的MFC软件界面开发工作.公司为某不景气的国企研究所.(喏,我的工作经验很水:1是方向不对:2是行业有偏差).然后目前是在寻找Pyth ...

  6. 利用vue-cli设置反向代理解决跨域问题

    在config文件夹下面的index.js里,添加如下代码即可 proxyTable: { '/apis': { //将接口域名印射为/apis target: 'http://192.168.3.1 ...

  7. Consul部署架构

    Consul 使用 Raft 算法来保证一致性, 比复杂的 Paxos 算法更直接,用于实现分布式系统的服务发现与配置. 应用Consul提供的服务需要建立Consul集群.在Consul方案中,每个 ...

  8. Django-- KindEditor 富文本编辑器使用

    KindEditor是一款还不错的开源的HTML可视化编辑器,主要用于让用户在网站上获得所见即所得编辑效果,兼容IE.Firefox.Chrome.Safari.Opera等主流浏览器.之所以推荐这一 ...

  9. Unable to convert MySQL date/time value to System.DateTime问题解决方案

    原因:可能是该字段(date/datetime)的值默认缺省值为:0000-00-00/0000-00-00 00:00:00,这样的数据读出来转换成System.DateTime时就会有问题: 解决 ...

  10. 软件加密工具-Virbox 开发者工具盒

    功能 Virbox 开发者工具盒是由深思数盾研发的一套软件加密工具,将加壳工具.API文档及操作流程文档等集成在一起,方便软件开发者使用. 您可以通过 Virbox 开发者工具盒实现: dll.exe ...