100. Same Tree

 
 My Submissions
 
  • Total Accepted: 159908
  • Total Submissions: 358174
  • Difficulty: Easy
  • Contributors: Admin

 

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

 

 

 

code:

/**
 * 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 q==p;
        if((p->val==q->val)&&isSameTree(p->left,q->left)&&isSameTree(p->right,q->right))
        return true;
        return false;
    }
};

 

相关文章:

  • 2021-05-20
  • 2021-07-30
  • 2022-12-23
  • 2022-12-23
  • 2021-08-02
  • 2021-12-26
  • 2021-10-06
猜你喜欢
  • 2021-09-13
  • 2022-01-05
  • 2021-08-18
  • 2022-03-09
  • 2021-09-04
  • 2021-09-02
相关资源
相似解决方案