Skip to content

Conversation

@ashokdey
Copy link
Member

@ashokdey ashokdey commented Oct 10, 2019

🌳 What's Inside?

  • findAncestors() to find all the ancestors of a given node.
  • findHeightOfBST() to find all the ancestors of a given node.
  • findKNodes() to find all the nodes k distance away from the root.

@ashokdey ashokdey requested a review from TheSTL October 10, 2019 13:42
@ashokdey ashokdey self-assigned this Oct 10, 2019
@ashokdey ashokdey changed the title New Problem - Find Ancestor of given Node New Problem - Find Height of BST & Ancestor of given Node Oct 10, 2019
@ashokdey ashokdey changed the title New Problem - Find Height of BST & Ancestor of given Node 3 New Problems on BST Oct 10, 2019
Copy link
Member

@TheSTL TheSTL left a comment

Choose a reason for hiding this comment

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

If a node is not found then return array should be empty in find-ancestors but it return array values where it visited.

@ashokdey
Copy link
Member Author

I have re-implemented it.

Copy link
Member

@TheSTL TheSTL left a comment

Choose a reason for hiding this comment

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

find-ancestors search whole BST tree. Implementation should be done in O(logn(n)) time.
You can use this code...

function findAncestors(root, value) {
  /**
   * search the given node and meanwhile
   * keep pushing the visited nodes
   */
  if (root === null) return false;

  if (value < root.value) {
    const left = findAncestors(root.leftChild, value);
    if (left) {
      return [...left, root.value];
    }
    return false;
  }

  if (value > root.value) {
    const right = findAncestors(root.rightChild, value);
    if (right) {
      return [...right, root.value];
    }
    return false;
  }

  if (value === root.value) return [];

  return false;
}

@ashokdey
Copy link
Member Author

find-ancestors search whole BST tree. Implementation should be done in O(logn(n)) time.
You can use this code...

function findAncestors(root, value) {
  /**
   * search the given node and meanwhile
   * keep pushing the visited nodes
   */
  if (root === null) return false;

  if (value < root.value) {
    const left = findAncestors(root.leftChild, value);
    if (left) {
      return [...left, root.value];
    }
    return false;
  }

  if (value > root.value) {
    const right = findAncestors(root.rightChild, value);
    if (right) {
      return [...right, root.value];
    }
    return false;
  }

  if (value === root.value) return [];

  return false;
}

I am both sad and happy, happy coz at least reached the best way using recursion in the first go, sad because I messed up with the edge case. 👥

Copy link
Member

@TheSTL TheSTL left a comment

Choose a reason for hiding this comment

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

💯

@ashokdey ashokdey merged commit e78eae5 into master Oct 11, 2019
@ashokdey ashokdey deleted the trees branch October 11, 2019 07:26
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.

3 participants