100. Same Tree

原创 2016年05月30日 19:16:31

二十八号的。。。

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.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        if(p==NULL&&q==NULL)
            return true;
        if(p==NULL)
            return false;
        if(q==NULL)
            return false;
        if(p->val!=q->val)
            return false;
        bool t1,t2;
        t1=isSameTree(p->left,q->left);
        t2=isSameTree(p->right,q->right);
        if(t1==false||t2==false)
            return false;
        return true;
    }
};


相关文章推荐

LeetCode 100 Same Tree(相同树判断)(二叉树、递归、栈和队列、深搜和宽搜)

翻译给定两个二叉树,写一个函数检查他们是否相等。两个二叉树如果结构上相同并且有相同的值,那么就认定他们是相等的。原文Given two binary trees, write a function t...
  • NoMasp
  • NoMasp
  • 2016年01月11日 13:27
  • 2174

【Leetcode】100.Same Tree

方法一:递归 思路: (1)若两棵树都为空,则返回true。 (2)若其中一棵树为空而另一棵树不为空,则返回false。 (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 ...
  • NOadu
  • NOadu
  • 2017年06月25日 11:47
  • 155

100. Same Tree [easy] (Python)

题目链接https://leetcode.com/problems/same-tree/题目原文 Given two binary trees, write a function to check...

Leetcode 100. Same Tree

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

leetcode No100. Same Tree

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

[LeetCode]100.Same Tree

【题目】 Same Tree  Total Accepted: 4943 Total Submissions: 11464My Submissions Given two binary...

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

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

[Leetcode 100] Same Tree

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

【LeetCode OJ 100】Same Tree

题目:Given two binary trees, write a function to check if they are equal or not. Two binary trees are...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:100. Same Tree
举报原因:
原因补充:

(最多只允许输入30个字)