100. Same Tree

100. Same Tree

My Submissions
Question
Total Accepted: 100129  Total Submissions: 236623  Difficulty: Easy

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.

Subscribe to see which companies asked this question

Hide Tags
  Tree Depth-first Search

朴素的递归思想:


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
 //思路首先:只要p和q都不为null且val相等就继续判断p的左子树和q的左子树以及p的右子树和q的右子树
 //如果出现某子树存在而另一颗树的相应子树却不存在,则返回false
class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        if(p==NULL&&q==NULL)  
            return true;  
        else if(p==NULL&&q!=NULL)  
            return false;  
        else if(p!=NULL&&q==NULL)  
            return false;  
        else if(p!=NULL&&q!=NULL && p->val!=q->val)  
            return false;  
        else  
            return (isSameTree(p->left,q->left))&&(isSameTree(p->right,q->right));  
    }
};


广度优先迭代遍历是否一样:

/**
 * 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(q==NULL && p ==NULL)  
        return true; 
      if(q == NULL && p!=NULL || q != NULL && p==NULL)    
            return false;
      //广度优先遍历两个子树是否一样  
      queue<TreeNode*> que1,que2;    
      TreeNode* curNode1=p;
      TreeNode* curNode2=q;      
      que1.push(curNode1);  
      que2.push(curNode2);
      while (!que1.empty()&&!que2.empty())    
      {      
         curNode1=que1.front();//出队首元素      
         que1.pop();//删除队首元素  
         curNode2=que2.front();//出队首元素      
         que2.pop();//删除队首元
         if(!(curNode1->val==curNode2->val))
            return false;
         if(curNode1->left!=NULL && curNode2->left!=NULL)    
         {   
            que1.push(curNode1->left);    
            que2.push(curNode2->left); 
         }
         
         if(curNode1->right!=NULL && curNode2->right!=NULL)    
         {     
             que1.push(curNode1->right);
             que2.push(curNode2->right); 
         }
         if(curNode1->left == NULL && curNode2->left!=NULL || curNode1->left != NULL && curNode2->left==NULL)    
            return false;
            
         if(curNode1->right == NULL && curNode2->right!=NULL || curNode1->right != NULL && curNode2->right==NULL)    
            return false;
      }  
      return true;  
    }
};


前序式深度优先搜索来做

/**
 * 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(q==NULL && p ==NULL)  
        return true; 
      if(q == NULL && p!=NULL || q != NULL && p==NULL)    
            return false;
      //前序式深度优先遍历两个子树是否一样  
      stack<TreeNode*> stk1,stk2;    
      TreeNode* curNode1=p;
      TreeNode* curNode2=q;      
      stk1.push(curNode1);  
      stk2.push(curNode2);
      while (!stk1.empty()&&!stk2.empty())    
      {      
         curNode1=stk1.top();//出队首元素      
         stk1.pop();//删除队首元素  
         curNode2=stk2.top();//出队首元素      
         stk2.pop();//删除队首元
         if(!(curNode1->val==curNode2->val))
            return false;
         if(curNode1->left!=NULL && curNode2->left!=NULL)    
         {   
            stk1.push(curNode1->left);    
            stk2.push(curNode2->left); 
         }
         
         if(curNode1->right!=NULL && curNode2->right!=NULL)    
         {     
             stk1.push(curNode1->right);
             stk2.push(curNode2->right); 
         }
         if(curNode1->left == NULL && curNode2->left!=NULL || curNode1->left != NULL && curNode2->left==NULL)    
            return false;
            
         if(curNode1->right == NULL && curNode2->right!=NULL || curNode1->right != NULL && curNode2->right==NULL)    
            return false;
      }  
      return true;  
    }
};


注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50507131

原作者博客:http://blog.csdn.net/ebowtang


你可能感兴趣的:(LeetCode,数据结构,算法,面试,ACM)