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){
if(isSameTree(p.left, q.left) && isSameTree(p.right, q.right)){
if(p.val == q.val) return true;
else return false;
}
else return false;
}
else return false;
}
}

LeetCode OJ 100. Same Tree的更多相关文章

  1. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  2. 【LEETCODE OJ】Binary Tree Postorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...

  3. 【LeetCode OJ】Same Tree

    Problem Link: https://oj.leetcode.com/problems/same-tree/ The following recursive version is accepte ...

  4. 【LeetCode OJ】Symmetric Tree

    Problem Link: https://oj.leetcode.com/problems/symmetric-tree/ To solve the problem, we can traverse ...

  5. 【LeetCode OJ】Binary Tree Level Order Traversal

    Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ Traverse the tree ...

  6. 【LeetCode OJ】Binary Tree Zigzag Level Order Traversal

    Problem Link: https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Just BFS fr ...

  7. 【LeetCode OJ】Binary Tree Level Order Traversal II

    Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ Use BFS from th ...

  8. 【LeetCode OJ】Binary Tree Maximum Path Sum

    Problem Link: http://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ For any path P in a bina ...

  9. 【LEETCODE OJ】Binary Tree Preorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ Even iterative solutio ...

随机推荐

  1. java多线程并发编程

    Executor框架 Executor框架是指java 5中引入的一系列并发库中与executor相关的一些功能类,其中包括线程池,Executor,Executors,ExecutorService ...

  2. OVERLAPPED相关的socket函数介绍

    OVERLAPPED相关的socket函数介绍 上一篇文章介绍了<Windows核心编程>OVERLAPPED结构与内核对象IOCompletionPort相关概念,见http://www ...

  3. mysql for windows zip版安装

    1.将mysql_5.6.24_winx64.zip 解压到文件夹 2.增加环境变量 3.修改mysql配置文件 将mysql根目录下的my-default.ini 复制一份更名为 my.ini.修改 ...

  4. Arrar.prototype.map()的用法

    ---恢复内容开始--- map() 方法返回一个由原数组中的每个元素调用一个指定方法后的返回值组成的新数组. array.map(callback[, thisArg]) 注:[]在语法中[]内的内 ...

  5. AUC计算方法总结

    一.roc曲线 1.roc曲线:接收者操作特征(receiveroperating characteristic),roc曲线上每个点反映着对同一信号刺激的感受性. 横轴:负正类率(false pos ...

  6. jquery 里 $(this)的用法

    当遇到循环table时,查看其中的td.tr属性和值会有一点的麻烦.此时就必须使用$(this)来解决这一类的问题了. 1.直接使用 2.间接使用 <table> <?php for ...

  7. 第六十六节,htnl音频视频

    htnl音频视频 学习要点:     1.音频和视频概述     2.video视频元素     3.audio音频元素 本章主要探讨HTML5中音频和视频元素,通过这两个原生的媒体元素向HTML页面 ...

  8. eclipse中git插件配置 编辑

    一.Eclipse上安装GIT插件EGit EGit插件地址:http://download.eclipse.org/egit/updates OK,随后连续下一步默认安装就可以,安装后进行重启Ecl ...

  9. js常见的判断移动端或者pc端或者安卓和苹果浏览器的方法总结

    1.js常见的判断移动端或者pc端或者安卓和苹果浏览器的方法总结 : http://www.haorooms.com/post/js_pc_iosandmobile 2.Js判断客户端是否为PC还是手 ...

  10. PAT 团体程序设计天梯赛-练习集 L2-009. 抢红包

    没有人没抢过红包吧…… 这里给出N个人之间互相发红包.抢红包的记录,请你统计一下他们抢红包的收获. 输入格式: 输入第一行给出一个正整数N(<= 104),即参与发红包和抢红包的总人数,则这些人 ...