DEV Community 👩‍💻👨‍💻

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Bottom view of Binary Tree

const getbottomView = function (root) {
  let map = new Map();
  let ans = [];
  // Function to return a list containing the bottom view of the given tree.
  const traversal = (root) => {
    if (root == null) return ans;

    let q = [];
    root.hd = 0;
    q.push(root);
    while (q.length) {
      let temp = q.shift();
      let hd = temp.hd;
      map.set(hd, temp.data);
      if (temp.left != null) {
        temp.left.hd = hd - 1;
        q.push(temp.left);
      }
      if (temp.right != null) {
        temp.right.hd = hd + 1;
        q.push(temp.right);
      }
    }
  };

  traversal(root);
  // sort by key
  const mapSort3 = new Map([...map.entries()].sort());

  for (let [key, value] of mapSort3) {
    ans.push(value);
  }
  return ans;
};


Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Timeless DEV post...

Git Concepts I Wish I Knew Years Ago

The most used technology by developers is not Javascript.

It's not Python or HTML.

It hardly even gets mentioned in interviews or listed as a pre-requisite for jobs.

I'm talking about Git and version control of course.