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

Compare Binary Trees BFS solution #59

Open
amsuarez99 opened this issue Aug 9, 2023 · 0 comments
Open

Compare Binary Trees BFS solution #59

amsuarez99 opened this issue Aug 9, 2023 · 0 comments

Comments

@amsuarez99
Copy link

amsuarez99 commented Aug 9, 2023

Just wanted to check how BFS solution would look like, but it is much more convoluted :sad_pepe:

import Queue from "./Queue";

export default function compare(
  a: BinaryNode<number> | undefined,
  b: BinaryNode<number> | undefined,
): boolean {
  const queue = new Queue<
    [BinaryNode<number> | undefined, BinaryNode<number> | undefined]
  >();
  queue.enqueue([a, b]);

  while (queue.length) {
    const curr = queue.deque();
    if (!curr) {
      continue;
    }

    if (curr[0] === undefined && curr[1] === undefined) {
      continue;
    }

    if (curr[0] === undefined || curr[1] === undefined) {
      return false;
    }

    if (curr[0].value !== curr[1].value) {
      return false;
    }

    queue.enqueue([curr[0].left, curr[1].left]);
    queue.enqueue([curr[0].right, curr[1].right]);
  }
  return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant