关闭

Same Tree

标签: javaleetcodetree
120人阅读 评论(0) 收藏 举报
分类:

题目描述:

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.

这个题目比较简单,2分钟就应该写出来。

public class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if((p==null)^(q==null)||(p!=null&&q!=null&&p.val!=q.val))
        	return false;
        if(p==null&&q==null)
        	return true;
        return isSameTree(p.left, q.left)&&isSameTree(p.right, q.right);
    }
}

0
0
查看评论

Same Tree -- LeetCode

原题链接: http://oj.leetcode.com/problems/same-tree/  这道题是树的题目,属于最基本的树遍历的问题。问题要求就是判断两个树是不是一样,基于先序,中序或者后序遍历都可以做完成,因为对遍历顺序没有要求。这里我们主要考虑一下结束条件,如果两个...
  • linhuanmars
  • linhuanmars
  • 2014-04-03 04:07
  • 9491

100. Same Tree [easy] (Python)

题目链接https://leetcode.com/problems/same-tree/题目原文 Given two binary trees, write a function to check if they are equal or not. Two binary trees a...
  • coder_orz
  • coder_orz
  • 2016-05-13 22:13
  • 1080

LeetCode-Same Tree(java)

t Same Tree  Total Accepted: 16148 Total Submissions: 38995My Submissions Given two binary trees, write a funct...
  • disappearedgod
  • disappearedgod
  • 2014-05-14 09:56
  • 1162

Leetcode 树 Same Tree

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Same Tree  Total Accepted: 15922 Total Submissions: 38418 Given two ...
  • zhsenl
  • zhsenl
  • 2014-05-11 17:01
  • 779

【LeetCode】100. Same Tree 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/problems/same-tree/ Given two binary trees, write a ...
  • crazy1235
  • crazy1235
  • 2016-05-30 22:06
  • 8094

【LeetCode】【Python题解】Same Tree

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 identica...
  • u011613729
  • u011613729
  • 2014-07-20 20:01
  • 3212

[LeetCode-100]Same Tree(java)

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 ...
  • xionghuixionghui
  • xionghuixionghui
  • 2016-10-08 20:48
  • 384

LeetCode 100 — Same Tree(C++ Java Python)

题目:http://oj.leetcode.com/problems/same-tree/ Given two binary trees, write a function to check if they are equal or not. Two binary trees are cons...
  • dragon_dream
  • dragon_dream
  • 2014-04-01 20:22
  • 1877

关于Leetcode中Same Tree一题的理解

题目如下: 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...
  • zsy112371
  • zsy112371
  • 2016-08-21 20:41
  • 235

100. Same Tree Leetcode Python

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...
  • hyperbolechi
  • hyperbolechi
  • 2015-01-15 23:33
  • 412
    个人资料
    • 访问:63458次
    • 积分:2768
    • 等级:
    • 排名:第15331名
    • 原创:215篇
    • 转载:37篇
    • 译文:0篇
    • 评论:6条
    最新评论