题目描述:

Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

解题思路:

递归调用比较左子树和右子树以及该节点。

代码描述:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if( p == null && q == null)
return true;
else if(p == null || q == null)
return false;
else
return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}

  

Java [Leetcode 100]Same Tree的更多相关文章

  1. LeetCode 100. Same Tree (判断树是否完全相同)

    100. Same Tree Given two binary trees, write a function to check if they are the same or not. Two bi ...

  2. leetcode 100. Same Tree、101. Symmetric Tree

    100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...

  3. leetcode 100 Same Tree ----- java

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  4. Java for LeetCode 100 Same Tree

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  5. LeetCode 100. Same Tree (相同的树)

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  6. [LeetCode] 100. Same Tree 相同树

    Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...

  7. leetcode 100. Same Tree

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  8. Java [Leetcode 94]Binary Tree Inorder Traversal

    题目描述: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given bina ...

  9. Java [Leetcode 144]Binary Tree Preorder Traversal

    题目描述: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given bin ...

随机推荐

  1. zhuan:ubuntu下安装Apache2+php+Mysql

    from: http://www.cnblogs.com/lynch_world/archive/2012/01/06/2314717.html ubuntu下安装Apache+PHP+Mysql 转 ...

  2. 遍历字典时用与不用iter的区别

    遍历字典时用与不用iter的区别 遍历字典的时候一般会用这三个方法:keys(),values(),items() 同时,它们各自都有升级版的方法:iterkeys(),itervalues(),it ...

  3. WPF自定义控件之带倒计时的按钮--Button

    1.说明 之前做过一个小项目,点击按钮,按钮进入倒计时无效状态,计时完成后,恢复原样,现在就实现该效果---带倒计时的按钮 2.效果 1)正常状态               2)MouseOver( ...

  4. main函数的正确格式

    main函数称之为主函数,一个C程序总是从main()函数开始执行的.在关于C语言的网贴和图书中,可以看到main函数的多种格式,这些格式,有的是正确的,有的是不正确的,为了避免错误,现归纳整理如下. ...

  5. Linux VM 设置静态ip地址上网

    因为是路由器共享上网,VM每次都是通过DHCP方式自动获取ip地址,连接Linux VM时ip地址经常变,很麻烦.现在把VM设置静态ip的方法总结一下,以免以后忘了. 1. VM上网方式设置为桥接. ...

  6. vi使用教程

    Vi有3种模式: 命令模式——命令操作 插入模式——进入vi之后,输入i/a/o,按Esc键,进入命令模式 编辑模式——:set nu, 以回车结束 1.插入 a - 光标后插入 A - 本行末尾插入 ...

  7. 强连通分量Tarjan模板

    #include<iostream> #include<stdio.h> #include<string.h> #include<stack> #inc ...

  8. 【转】代码控制UI,View

    [转]Android 步步为营 第5营 代码控制UI,View   http://www.cnblogs.com/vivid-stanley/archive/2012/08/22/2651399.ht ...

  9. win8连接蓝牙听歌

    今天买了一个蓝牙耳机,琢磨着在win8.1上听一下,可是折腾了一阵时间,现在把最佳配置方式写出来,希望对朋友有所帮助 确保win8的蓝牙驱动已经安装完毕,并且开启蓝牙,win8,设置--右下角更改电脑 ...

  10. ACCESS数据库操作教程

    网易学院视频教程: 上:http://tech.163.com/06/0621/17/2K5K0C2200091U6J.html中:http://tech.163.com/06/0621/17/2K5 ...