Duplicates in binary tree

Given a binary tree of integers. Return whether it contains a duplicate sub-tree of size 2 or more.

All node values lie between 0 and 9 inclusive.

Return 1 if it contains a duplicate sub-tree of size 2 or more else return 0.

Note: Two same leaf nodes are not considered as a subtree, As the size of a leaf node is one.

Constraints

1 <= Number of nodes in binary tree <= 10000
0 <= node values <= 9

For Example

Input 1:
            1
          /   \
         2     6
        / \     \
       4   5     2
                / \
               4   5

Output 1:
    1
    
    Duplicate Subtree     2
                         / \
                        4   5

Input 2:
            1
           /  \
          2    3
           \
            4
             \
              5
Output 2:
    0
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Duplicates in binary tree on Interview Code Editor
Sign Up
to access hints and editorial solutions for Duplicates in binary tree

Discussion


Loading...
Click here to start solving coding interview questions