题目描述:

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. WPF中禁止WebBrowser控件打开新窗口

    一.针对纯WPF的WebBrowser控件: <summary> Suppress Script Errors In WPF WebBrowser </summary> pub ...

  2. Javacript 客户端保存数据[ locaStorage ]

    1.通常程序员们会使用Cookie进行一些小量的数据储存在客户端浏览器,但孰不知这样会造成不必要的带宽浪费 ,可使用 js 中的 locaStorage 来替代cookie进行存储,但不支持IE8以下 ...

  3. UILabel 根据内容的多少来计算label的frame

    self.label.text = @"...."; 计算 frame 的最新方法 //1.设置lable最大显示行数 self.label.numberOfLines = 0; ...

  4. POJ1734 - Sightseeing trip

    DescriptionThere is a travel agency in Adelton town on Zanzibar island. It has decided to offer its ...

  5. Extjs4.2——bbar的默认类型(xtype)

    bbar:在Panel经常使用的工具栏 如下面的示例——这将牵涉本文要追寻的问题:在下面的Panel中的bbar第一个组件明确指定的xtype:'button',第二个没有明确指出,那么它是何类型,为 ...

  6. 团体程序设计天梯赛-练习集L2-005. 集合相似度

    L2-005. 集合相似度 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定两个整数集合,它们的相似度定义为:Nc/Nt*1 ...

  7. OD之窗口界面

  8. [Database]Operators

    Arithmetic Operators +,-,×,÷ Comparison Operators =,<>,!=,>,<,>=,<= BETWEEN,IN,IS ...

  9. 【leetcode】Intersection of Two Linked Lists(easy)

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  10. Tomcat多次部署

    http://blog.csdn.net/knityster/article/details/6300804