Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create potd_24_10_2024.cpp #46

Merged
merged 1 commit into from
Oct 25, 2024
Merged

Create potd_24_10_2024.cpp #46

merged 1 commit into from
Oct 25, 2024

Conversation

PriyanshK09
Copy link
Contributor

@PriyanshK09 PriyanshK09 commented Oct 24, 2024

Problem of the Day (October 24)

Description

Please include a summary of the changes and the related issue(s) this pull request addresses. Include any relevant context or background information.
The problem asks whether two binary trees are flip equivalent. Two trees are flip equivalent if they are the same when flipped at any node, meaning that at any node, we can swap the left and right subtrees.
My first thought is that we need to check the values of the nodes in a recursive manner. At each node, we either check if both left and right subtrees are the same without flipping, or we check if they are the same with flipping (i.e., left subtree of one tree corresponds to the right subtree of the other and vice versa).

Fixes: #45 (replace with the issue number, if applicable)

Use [x] to represent a checked (ticked) box.✅
Use [ ] to represent an unchecked box.❌

Type of Change

  • Question Added
  • Solution Added
  • Other (please specify):

How to Test

We can solve this using recursion. At each step:
- If both nodes are NULL, they are trivially equivalent.
- If only one of them is NULL or their values are different, they cannot be flip equivalent.

Recursively check the following two possibilities:
    - The left subtree of the first tree is flip equivalent to the left subtree of the second tree, and the right subtree of the first tree is flip equivalent to the right subtree of the second tree.
    - The left subtree of the first tree is flip equivalent to the right subtree of the second tree, and the right subtree of the first tree is flip equivalent to the left subtree of the second tree.

By considering both cases, we ensure that we account for potential flips at every node.

Checklist

  • I have performed a self-review of my code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation (if applicable).
  • My changes generate no new warnings.
  • I have added tests to cover my changes (if applicable).
  • All new and existing tests pass.

Additional Notes

Complexity
Time complexity: $$O(n)$$
Space complexity: $$O(h)$$

Please add any other information that is relevant to this pull request, including potential risks, alternative solutions considered, or future improvements.

Problem of the Day (October 24)
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job, @PriyanshK09! Thanks for creating the pull request.
Soon the maintainers/owner will review it and provide you with feedback/suggestions.
Make sure to star this awesome repository and follow the account!

@Gyanthakur
Copy link
Owner

You are not following my account.

@PriyanshK09
Copy link
Contributor Author

You are not following my account.

Done!

@Gyanthakur Gyanthakur merged commit 1f091a0 into Gyanthakur:main Oct 25, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Solution to 24 October Leetcode POTD
2 participants