Old discuss is read-only now. Please go to New LeetCode Discuss for your questions and answers!

User account in old discuss will not integrate to the new one, but new discuss is integrated with new online judge, which means, if you already have an account in new online judge, you can access new discuss immediately!

If you want to ask for question relevant to the posts in old discuss, please copy content as part of your question, only old discuss link is NOT ALLOWED!

Please read the FAQ in new LeetCode Discuss to help yourself making the best use of Discuss!

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.

asked 03 Sep '12, 23:48

1337c0d3r's gravatar image

1337c0d3r ♦♦
1.2k1284171
accept rate: 2%

closed 25 Oct '13, 06:41

The question has been closed for the following reason "bulk close" by 1337c0d3r 25 Oct '13, 06:41


class Solution {

public:

  bool checkSub(TreeNode *p, TreeNode *q) {
      if(p == NULL && q == NULL) return true;
      if(p == NULL || q == NULL) return false;
      if(p->val == q->val && checkSub(p->left, q->left) && checkSub(p->right, q->right))
          return true;
      return false;
  }
  bool isSameTree(TreeNode *p, TreeNode *q) {
      return checkSub(p, q);
  }
};
link
This answer is marked "community wiki".

answered 31 Dec '12, 06:32

ktu's gravatar image

ktu
14525
accept rate: 0%

edited 31 Dec '12, 21:40

class Solution {
public:
bool isSameTree(TreeNode *p, TreeNode *q) {
    if(!p && !q)
        return true;
    if (p && q)
    {
        if(p->val == q->val)
            return (isSameTree(p->left, q->left) && isSameTree(p->right, q->right));
        else
            return false;
    }
    else
        return false;   
 }
};
link

answered 26 Jan '13, 20:07

BlackMamba's gravatar image

BlackMamba
11124
accept rate: 0%

p和q都为null,return true; p和q都不为null,递归进行判断; p和q之一为null,return false。

(26 Jan '13, 20:08) BlackMamba BlackMamba's gravatar image
public class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        // Start typing your Java solution below
        // DO NOT write main() function
        if(p==null&&q==null) return true;
        if(p==null||q==null) return false;
        if(p.val!=q.val) return false;
        boolean left=isSameTree(p.left,q.left);
        if(!left) return left;
        boolean right=isSameTree(p.right,q.right);
        return right;
    }
}
link

answered 26 Jan '13, 20:39

Pan's gravatar image

Pan
364
accept rate: 0%

class Solution 
{
public:
bool isSameTree(TreeNode *p, TreeNode *q) 
  {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    if( !p && !q) return true;
    if( p && q && (p->val  == q->val)) return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
    else 
        return false;
  }
};
link

answered 28 Jan '13, 21:12

spooky's gravatar image

spooky
124
accept rate: 0%

bool isSameTree(TreeNode *p, TreeNode *q) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    if(p==NULL && q==NULL) return true;
    else if(p != NULL && q != NULL){
        return (p->val == q->val &&
                isSameTree(p->left, q->left) &&
                isSameTree(p->right, q->right));
    }
    else return false;
}
link

answered 22 Feb '13, 09:30

27606's gravatar image

27606
83
accept rate: 0%

bool isSameTree(TreeNode p, TreeNode q) {

    if(!p && !q) return true;
    if(!p || !q) return false;
    if(p->val==q->val) return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
    else return false;

}

link

answered 20 Jun '13, 21:14

varunvats32's gravatar image

varunvats32
213
accept rate: 0%

Following is the O(min(m, n)) Solution where m = number of nodes in tree1 and n = number of nodes in tree2.

The recursion uses the implicit stack. In order to implement it iteratively we need to use in-order traversal logic.

following is the code.

class Solution {
public:
    bool isSameTree(TreeNode *p, TreeNode *q) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (p == NULL && q == NULL)
            return true;
        if (p == NULL && q != NULL)
            return false;
        else if (p != NULL && q == NULL)
            return false;
        else if (p->val != q->val)
            return false;
        else
            return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);

    }
};
link

answered 22 Jun '13, 08:50

Laiq%20Ahmed's gravatar image

Laiq Ahmed
01
accept rate: 0%

public boolean isSameTree(TreeNode p, TreeNode q) {
    // Start typing your Java solution below
    // DO NOT write main() function
    if(p==null && q==null)
        return true;
    else if(p==null || q==null)
        return false;
    else if(p.val!=q.val)
        return false;
    else
        return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
link

answered 12 Aug '13, 15:20

AlienKnight's gravatar image

AlienKnight
214
accept rate: 0%

boolean sameTree(Node node1, Node node2)
{
    //base case
    if (node1 == null && node2==null)
    {
        return true;
    }
    else if ((node1 == null && node2 != null) || (node1 != null && node2 == null))
    {
        return false;
    }

    //now node1 != null && node2 != null
    if (node1.data != node2.data)
    {
        return false;
    }

   //node1.data == node2.data

   boolean leftEqual = sameTree(node1.leftChild, node2.leftChild);
   boolean rightEqual = sameTree(node1.rightChild, node2.rightChild);

   return leftEqual && rightEqual;
}
link

answered 21 Oct '13, 23:03

e230217's gravatar image

e230217
213
accept rate: 0%

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • Indent code by 4 spaces.
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×133

Asked: 03 Sep '12, 23:48

Seen: 1,622 times

Last updated: 25 Oct '13, 06:41