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

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

Example 1:

Input:     1         1
/ \ / \
2 3 2 3 [1,2,3], [1,2,3] Output: true

Example 2:

Input:     1         1
/ \
2 2 [1,2], [1,null,2] Output: false

Example 3:

Input:     1         1
/ \ / \
2 1 1 2 [1,2,1], [1,1,2] Output: false

给2个二叉树,写一个函数来判断它们是否是相同的。

解法:递归。当前节点相等,且他们左子树和右子树都相等,要考虑到树为Null的情况。基于先序,中序或者后序遍历都可以做完成,因为对遍历顺序没有要求。这里主要考虑一下结束条件,如果两个结点都是null,也就是到头了,那么返回true。如果其中一个是null,说明在一棵树上结点到头,另一棵树结点还没结束,即树不相同,或者两个结点都非空,并且结点值不相同,返回false。最后递归处理两个结点的左右子树,返回左右子树递归的与结果即可。这里使用的是先序遍历,算法的复杂度跟遍历是一致的,如果使用递归,时间复杂度是O(n),空间复杂度是O(logn)。

Java:

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

Python:

class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None class Solution:
# @param p, a tree node
# @param q, a tree node
# @return a boolean
def isSameTree(self, p, q):
if p is None and q is None:
return True if p is not None and q is not None:
return p.val == q.val and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) return False  

C++:

class Solution {
bool check(TreeNode* p, TreeNode* q) {
// 若两个指针均为空
if (!p && !q) return true;
// 若只有一个指针为空
if (!p || !q) return false;
// 两个指针都不为空,则比较值
if (p -> val != q -> val) return false; // 递归比较子节点
return check(p -> left, q -> left) && check(p -> right, q -> right);
}
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
return check(p, q);
}
}; 

 

类似题目:

[LeetCode] 110. Balanced Binary Tree 平衡二叉树

 

All LeetCode Questions List 题目汇总

[LeetCode] 100. Same Tree 相同树的更多相关文章

  1. [LeetCode]100. Same Tree判断树相同

    dfs遍历一下判断 public boolean isSameTree(TreeNode p, TreeNode q) { if (p==null) { return q == null; } els ...

  2. 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 ...

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

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

  4. 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 the same or not. Two binary trees are ...

  6. LeetCode 100. Same Tree相同的树 (C++)

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

  7. [leetcode] 101. Symmetric Tree 对称树

    题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...

  8. leetcode 100. Same Tree

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

  9. 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 ...

随机推荐

  1. Codeforces B. Minimum Possible LCM(贪心数论)

    题目描述: B. Minimum Possible LCM time limit per test 4 seconds memory limit per test 1024 megabytes inp ...

  2. Spring Cloud Ribbon负载均衡(快速搭建)

    Spring Cloud Ribbon 是一个基于HTTP和TCP的客户端负载均衡工具,它基于Netflix Ribbon实现.通过 Spring Cloud 的封装, 可以让我们轻松地将面向服务的 ...

  3. C#WinForm无边框窗体移动----模仿鼠标单击标题栏移动窗体位置

    C#WinForm无边框窗体移动方法.模仿鼠标单击标题栏移动窗体位置 这里介绍俩种办法 方法一:直接通过修改窗体位置从而达到移动窗体的效果 方法二:直接伪装发送单击任务栏消息,让应用程序误以为单击任务 ...

  4. P4139 上帝与集合的正确用法[欧拉定理]

    题目描述 求 \[ 2^{2^{2\cdots}} ~mod ~p \] 简单题,指数循环节. 由于当\(b>=\psi(p)\)时,有 \[ a^b=a^{b ~mod~\psi(p)+\ps ...

  5. java技术思维导图(转载)

      在网上看到有个人总结的java技术的东东,觉得很好,就保存下来了,码农还真是累啊,只有不断的学习才能有所提高,才能拿更多的RMB啊. java技术思维导图 服务端思维导图 前端思维导图

  6. 我永远讨厌gch文件

    一个学期没写博客了. 今天写OOP作业见鬼了, 调了半天. 我写了一个match.h和一个match.cpp, 然后match.cpp里面#include"match.h", 然后 ...

  7. Wideband Direction of Arrival Estimation Based on Multiple Virtual Extension Arrays

    基于多重虚拟扩展阵列的宽带信号DOA估计[1]. 宽带DOA估计是阵列信号处理领域的一个重要研究方向.在DOAs估计的实际应用中,信号总是会被噪声破坏,在某些情况下,源信号的数量大于传感器的数量,因此 ...

  8. 2019-2020-1 20199302《Linux内核原理与分析》第八周作业

    一.上课学习笔记 1.shell作用:①运行程序 ②重定向(输入/输出重定向) ③可编程(写脚本) 执行一个c程序时,如果切进另一个进程,会进入该进程而切不回原进程,所以需要为调用的进程创一个子进程. ...

  9. codeforces1276A As Simple as One and Two

    C.As Simple as One and Two A. As Simple as One and Two time limit per test 3 seconds memory limit pe ...

  10. webuploader之大文件分段上传、断点续传

    文件夹数据库处理逻辑 public class DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject() ...