关闭

Leetcode 100. Same Tree

54人阅读 评论(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.

我的代码:

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 if(q == null && p != null){
            return false;
        }
        else if( p.val == q.val){
            if(isSameTree(p.left, q.left) == true && isSameTree(p.right, q.right) == true){
                return true;
            }
            else{
                return false;
            }
        }
        else{
            return false;
        }
    }
}
discuss中的更优解

public boolean isSameTree(TreeNode p, TreeNode q) {
    if(p == null && q == null) return true;
    if(p == null || q == null) return false;<span style="white-space:pre">	</span>//此处合并了我第2、3个else if
    return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}



0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

C++二叉树遍历总结\100. Same Tree

理论学习 概念介绍 遍历图解 遍历算法 代码实践 实现模板理论学习概念介绍二叉树的遍历分成前序、中序、后序遍历。前序就是访问结点的操作发生在遍历其左右子树之前。 中序就是访问结点的操作发生在遍历其左...
  • c602273091
  • c602273091
  • 2017-02-15 14:45
  • 194

【LeetCode】Same Tree 解题报告

Same Tree[LeetCode]https://leetcode.com/problems/same-tree/Total Accepted: 126017 Total Submissions:...
  • fuxuemingzhu
  • fuxuemingzhu
  • 2016-04-30 01:21
  • 176

关于Leetcode中Same Tree一题的理解

题目如下: Given two binary trees, write a function to check if they are equal or not. Two binary t...
  • zsy112371
  • zsy112371
  • 2016-08-21 20:41
  • 221

Leetcode Symmetric Tree 递归和非递归解法

和same tree题目差不多一样的解法。 1 recursively 递归法: 2 iteratively 非递归: 使用先序遍历,原来树的先序遍历的非递归写法也不简单。
  • kenden23
  • kenden23
  • 2014-02-16 01:11
  • 14313

LeetCode之Tree题目汇总

Balanced Binary TreeGiven a binary tree, determine if it is height-balanced.For this problem, a heig...
  • Yano_nankai
  • Yano_nankai
  • 2015-12-29 20:43
  • 695

leetcode 虐我篇之(十九)Same Tree

好久没来leetcode上面做题了,今天先从一道简单题目来
  • u013592466
  • u013592466
  • 2014-09-29 18:48
  • 348

算法课第2周第1题——100. Same Tree

题目描述: Given two binary trees, write a function to check if they are equal or not. Two binary tree...
  • bwstardust
  • bwstardust
  • 2017-02-28 01:05
  • 89

100. Same Tree和判断一个树是不是对称的思想一样

Given two binary trees, write a function to check if they are equal or not.Two binary trees are cons...
  • shanshanhi
  • shanshanhi
  • 2017-01-12 16:49
  • 127

LeetCode OJ 之 Same Tree (相同树的判断)

题目: Given two binary trees, write a function to check if they are equal or not. Two binary t...
  • u012243115
  • u012243115
  • 2014-11-15 13:52
  • 411

Leetcode-Symmetric Tree——判断二叉树是否对称

题目 Symmetric Tree  Total Accepted: 12814 Total Submissions: 40358My Submissions Given a bina...
  • disappearedgod
  • disappearedgod
  • 2014-04-20 01:00
  • 2057
    个人资料
    • 访问:687次
    • 积分:104
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章存档