100. Same Tree

原创 2016年08月29日 21:04:39

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.

主要是二叉树的bfs的运用。

/**
 * 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) {
         queue<TreeNode*> pque,qque;
         if (p&&q)
         {
             pque.push(p);
             qque.push(q);
         }
         else if (!p&&!q)  return true;
         else return false;
         while (!pque.empty()&&!qque.empty())
         {
             if (pque.front()->val!=qque.front()->val)
                 return false;
             else
             {
                 TreeNode* pp=pque.front(),*qq=qque.front();
                 pque.pop();
                 qque.pop();
                 if (pp->left&&qq->left)
                 {
                    pque.push(pp->left);
                    qque.push(qq->left);
                 }
                 else if(!pp->left&&!qq->left) {}
                 else return false;
                 if (pp->right&&qq->right)
                 {
                    pque.push(pp->right);
                    qque.push(qq->right);
                 }
                 else if(!pp->right&&!qq->right) {}
                 else return false;
             }
         }
         if (!pque.empty()||!qque.empty())
            return false;
         return true;
    }
};

中序遍历递归实现,用的是dfs。

/**
 * Definition for binary tree
 * 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 && !q)
            return true;
        else if(!p && q)
            return false;
        else if(p && !q)
            return false;
        else
        {
            if(p->val != q->val)
                return false;
            else
                return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
        }
    }
};



版权声明:本文为博主原创文章,未经博主允许不得转载。

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

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

【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
  • 166

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个字)