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. eclipse中的快捷键的使用

  2. js中级小知识

    1.作用域链 作用域:浏览器给js的一个生存环境(栈内存). 作用域链:js中的关键字var和function都可以提前声明和定义,提前声明和定义的放在我们的内存地址(堆内存)中.然后js从上到下逐行 ...

  3. Springboot的2种启动方式

    sprigboot既可以直接通过main方法启动,也可以在tomcat里启动,在main方法里启动很简单,直接run启动类的main方法就可以了. 在tomcat里启动是需要配置一下的,需要实现以下S ...

  4. gitlab提交代码

    cd existing_foldergit initgit remote add origin http://10.26.1.9/root/yunlian.gitgit add .git commit ...

  5. 用ASPOSE.Cells将HTML表格存为Excel

    前端生成的html表格经常需要导出到excel中,利用JS和Office控件可以做到,但仅限于IE,还要启用安全设置. 想找一个简单的办法将HTML内容直接转换成Excel文件,如果直接修改网页头信息 ...

  6. 泡泡一分钟:Cubic Range Error Model for Stereo Vision with Illuminators

    Cubic Range Error Model for Stereo Vision with Illuminators 带有照明器的双目视觉的三次范围误差模型 "链接:https://pan ...

  7. spring管理的类如何调用非spring管理的类

    spring管理的类如何调用非spring管理的类. 就是使用一个spring提供的感知概念,在容器启动的时候,注入上下文即可. 下面是一个工具类. import org.springframewor ...

  8. ios安装ipa与安卓安装apk

    ideviceinstaller -i .ipa包所在的路径 环境搭建:Mac上安装brew(brew里面有很多命令,可以安装自己想用的命令) 安装命令如下:curl -LsSf http://git ...

  9. Spring Boot 自动配置原理(精髓)

    一.自动配置原理(掌握) SpringBoot启动项目会加载主配置类@SpringBootApplication,开启@EnableAutoConfiguration自动配置功能 @EnableAut ...

  10. 判断是否滚动加载结束 用一个公共变量isScroll来控制

    如果还没达到最大页数,isScroll就一直是不变,ajax正常进行.如果达到最大页数,就不执行ajax操作了...