top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

Compare Two Binary Tree structures?

+3 votes

Given 2 binary search tree what we know is -
1. Both the trees have same number of elements.
2. Elements can be same or different.
3. We need to find out if the structure of both the trees is same or not. (only structure not the content)

posted Nov 30, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes
Best answer

check at each level if the node exist or not and traverse completely.

private boolean sameStructure(TreeNode ref, TreeNode otherRef) 
    if (otherRef == null && ref != null)
        return false;
    if (otherRef != null && ref == null)
        return false;
    if (otherRef == null && ref == null)
        return true;

    return sameStructureHelp(ref.left, otherRef.left) && 
           sameStructureHelp(ref.right, otherRef.right);
answer Dec 1, 2013 by Sanketi Garg
Similar Questions
+1 vote

How do you shuffle a binary tree without using any external data structures.
Note: With equal probability to every node.

0 votes

Given the root of a Binary Tree along with two integer values. Assume that both integers are present in the tree.
Find the LCA (Least Common Ancestor) of the two nodes with values of the given integers.
2 pass solution is easy. You must solve this in a single pass.

Contact Us
+91 9880187415
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Karnataka INDIA.