Skip to content

951. Flip Equivalent Binary Trees #743

Answered by kovatz
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We can use a recursive depth-first search (DFS). The idea is that two trees are flip equivalent if their root values are the same, and the subtrees are either the same (without any flips) or they become the same after flipping the left and right children at some nodes.

Plan:

  1. Base Cases:

    • If both root1 and root2 are null, they are trivially flip equivalent.
    • If only one of them is null, they can't be equivalent.
    • If the root values of root1 and root2 are different, they can't be equivalent.
  2. Recursive Case:

    • Recursively check two possibilities:
      1. The left subtree of root1 is flip equivalent to the left subtree of root2, and the right subtree of root1 is flip equivalent to the right subtre…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mah-shamim
Comment options

mah-shamim Oct 24, 2024
Maintainer Author

@kovatz
Comment options

kovatz Oct 24, 2024
Collaborator

Answer selected by mah-shamim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty hacktoberfest hacktoberfest
2 participants