Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8081523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:45:15+00:00 2026-06-05T16:45:15+00:00

Two trees are said to be identical if they contain same set of elements

  • 0

Two trees are said to be identical if they contain same set of elements but may have different structures.
e.g. 4,3,5 and 5,4,3

How to check whether the two trees are identical?

One approach i can think of is to use hashing. For each element in the first tree, the corresponding count is incremented. For each element in the second tree, the count is decremented. At the end, the the hash is empty, we are sure that trees are identical.
Time complexity: O(N)
Space complexity: O(N)

But, this approach doesn’t make use of whether tree is a BST or a simple BINARY TREE.

Approach 2: Take inorder traversal of both the trees in array. We are with two arrays having sorted data. Do a linear search to check whether arrays are identical are not.
Time complexity: O(N)
Space complexity: O(N)

But, I wanted to know that does there exist any better solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:45:17+00:00Added an answer on June 5, 2026 at 4:45 pm

    Your latter solution seems better than your former, as while the worst-case time for the latter is O(N), the best case (where the first elements in the in-order traversals differ) would be Ω(1), while for the former, best-case time would still be Ω(N) as you have to wait until the end to know for sure.

    As an optimization for the latter one, though, couldn’t you just use a pointer to the current element in each tree rather than making copies of all the data? The algorithm for in-order traversal of a tree (with natural ordering, at least) doesn’t require making any copies of the data. That way your space complexity would be O(1).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my rails application I have two trees that are identical but mounted in
I need to check two binary trees to see if they are similar... meaning
I have two development trees v3.3, and v3.4 in one SVN repository. They both
I have a merge function which takes time O(log n) to combine two trees
I have two large source trees. One of them has some out of date
I have two XML trees and would like to add one tree as a
In short: I want to generate two different source trees from the current one,
I have a domain domain.com and subdomain sub.domain.com Those two domains have different ftp
Suppose we have two people in a group. We maintain two GIT trees A
Assume that I have two AVL trees and that I know their respective sizes.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.