{"id":22513450,"url":"https://github.com/emahtab/binary-tree-top-view","last_synced_at":"2026-03-19T23:02:40.546Z","repository":{"id":260386378,"uuid":"881155811","full_name":"eMahtab/binary-tree-top-view","owner":"eMahtab","description":"Binary Tree Top View","archived":false,"fork":false,"pushed_at":"2024-10-31T02:13:10.000Z","size":3,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-02T03:19:14.916Z","etag":null,"topics":["binary-tree","hackerrank","top-view-binary-tree"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-31T02:07:27.000Z","updated_at":"2024-10-31T02:14:19.000Z","dependencies_parsed_at":"2024-10-31T03:18:07.524Z","dependency_job_id":"fc577b6c-397a-4697-8cf5-832f7aff0be6","html_url":"https://github.com/eMahtab/binary-tree-top-view","commit_stats":null,"previous_names":["emahtab/binary-tree-top-view"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbinary-tree-top-view","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbinary-tree-top-view/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbinary-tree-top-view/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbinary-tree-top-view/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/binary-tree-top-view/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245950639,"owners_count":20699103,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["binary-tree","hackerrank","top-view-binary-tree"],"created_at":"2024-12-07T03:12:23.217Z","updated_at":"2026-02-26T01:50:06.748Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Binary Tree Top View\n### https://www.hackerrank.com/challenges/tree-top-view/problem\n\nGiven a pointer to the root of a binary tree, print the top view of the binary tree.\n\nThe tree as seen from the top the nodes, is called the top view of the tree.\n\n```\nFor example :\n\n   1\n    \\\n     2\n      \\\n       5\n      /  \\\n     3    6\n      \\\n       4\n```\nTop View : 1, 2, 5, 6\n\nComplete the function  and print the resulting values on a single line separated by space.\n\n## Implementation :\n```java\nclass Solution {\n\n\t/* \n    \n    class Node \n    \tint data;\n    \tNode left;\n    \tNode right;\n\t*/\n    class TreeNode {\n        Node node;\n        int col;\n        TreeNode(Node node, int col){\n            this.node = node;\n            this.col = col;\n        }\n    }\n\n   public static void topView(Node root) {\n      Map\u003cInteger,Node\u003e map = new HashMap\u003c\u003e();\n      Queue\u003cTreeNode\u003e queue = new LinkedList\u003c\u003e();\n      int leftmostCol = Integer.MAX_VALUE;\n      int rightmostCol = Integer.MIN_VALUE;\n      queue.add(new TreeNode(root, 0));\n      while(!queue.isEmpty()) {\n          int size = queue.size();\n          for(int i = 1; i \u003c= size; i++) {\n              TreeNode treeNode = queue.remove();\n              int column = treeNode.col;\n              leftmostCol = Math.min(leftmostCol, column);\n              rightmostCol = Math.max(rightmostCol, column);\n              map.putIfAbsent(column, treeNode.node);\n              if(treeNode.node.left != null)\n                  queue.add(new TreeNode(treeNode.node.left, column -1 ));\n              if(treeNode.node.right != null)\n                  queue.add(new TreeNode(treeNode.node.right, column +1));    \n          }\n      }\n      for(int column = leftmostCol; column \u003c= rightmostCol; column++) {\n          System.out.print(map.get(column).data + \" \");\n      }\n   }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fbinary-tree-top-view","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fbinary-tree-top-view","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fbinary-tree-top-view/lists"}