{"id":22513881,"url":"https://github.com/emahtab/binary-tree-vertical-order-traversal","last_synced_at":"2026-03-19T23:02:50.912Z","repository":{"id":79525308,"uuid":"263401224","full_name":"eMahtab/binary-tree-vertical-order-traversal","owner":"eMahtab","description":"Binary Tree Vertical Order Traversal","archived":false,"fork":false,"pushed_at":"2022-05-01T07:36:35.000Z","size":51,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-02T03:25:47.016Z","etag":null,"topics":["bfs","binary-tree-traversal","leetcode","problem-solving","vertical-order-traversal"],"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":"2020-05-12T17:11:14.000Z","updated_at":"2022-05-01T07:36:38.000Z","dependencies_parsed_at":"2023-05-10T17:00:48.318Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/binary-tree-vertical-order-traversal","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbinary-tree-vertical-order-traversal","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbinary-tree-vertical-order-traversal/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbinary-tree-vertical-order-traversal/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbinary-tree-vertical-order-traversal/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/binary-tree-vertical-order-traversal/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245952147,"owners_count":20699437,"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":["bfs","binary-tree-traversal","leetcode","problem-solving","vertical-order-traversal"],"created_at":"2024-12-07T03:14:47.210Z","updated_at":"2025-10-05T02:32:18.963Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Binary Tree Vertical Order Traversal\n## https://leetcode.com/problems/binary-tree-vertical-order-traversal\n## https://www.lintcode.com/problem/651\n\nGiven a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bottom, column by column).\n\n#### Very Important 😯: If two nodes are in the same row and column, the order should be from left to right.\n\n![Binary Tree Vertical Order Traversal](vertical-order-traversal.JPG?raw=true \"Binary Tree Vertical Order Traversal\")\n\n```\nExamples 1:\n\nInput: [3,9,20,null,null,15,7]\n\n   3\n  /\\\n /  \\\n 9  20\n    /\\\n   /  \\\n  15   7 \n\nOutput:\n\n[\n  [9],\n  [3,15],\n  [20],\n  [7]\n]\nExamples 2:\n\nInput: [3,9,8,4,0,1,7]\n\n     3\n    /\\\n   /  \\\n   9   8\n  /\\  /\\\n /  \\/  \\\n 4  01   7 \n\nOutput:\n\n[\n  [4],\n  [9],\n  [3,0,1],\n  [8],\n  [7]\n]\nExamples 3:\n\nInput: [3,9,8,4,0,1,7,null,null,null,2,5] (0's right child is 2 and 1's left child is 5)\n\n     3\n    /\\\n   /  \\\n   9   8\n  /\\  /\\\n /  \\/  \\\n 4  01   7\n    /\\\n   /  \\\n   5   2\n\nOutput:\n\n[\n  [4],\n  [9,5],\n  [3,0,1],\n  [8,2],\n  [7]\n]\n```\n\n# Implementation :\n```java\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *     TreeNode right;\n *     TreeNode() {}\n *     TreeNode(int val) { this.val = val; }\n *     TreeNode(int val, TreeNode left, TreeNode right) {\n *         this.val = val;\n *         this.left = left;\n *         this.right = right;\n *     }\n * }\n */\nclass Solution {\n    class Node {\n        TreeNode treeNode;\n        int horizontalDistance;\n        Node(TreeNode treeNode, int horizontalDistance) {\n            this.treeNode = treeNode;\n            this.horizontalDistance = horizontalDistance;\n        } \n    }\n    \n    public List\u003cList\u003cInteger\u003e\u003e verticalOrder(TreeNode root) {\n        List\u003cList\u003cInteger\u003e\u003e res = new ArrayList\u003c\u003e();\n        if(root == null) return res;\n        Queue\u003cNode\u003e q = new LinkedList\u003c\u003e();\n        q.offer(new Node(root,0));\n        Map\u003cInteger,List\u003cInteger\u003e\u003e map = new HashMap\u003c\u003e();\n        int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;\n        while(!q.isEmpty()) {\n            Node current = q.poll();\n            List\u003cInteger\u003e list = map.getOrDefault(current.horizontalDistance, new ArrayList\u003cInteger\u003e());\n            list.add(current.treeNode.val);\n            map.put(current.horizontalDistance,list);\n            min = Math.min(min, current.horizontalDistance);\n            max = Math.max(max, current.horizontalDistance);\n            if(current.treeNode.left != null)\n                q.offer(new Node(current.treeNode.left, current.horizontalDistance - 1));\n            if(current.treeNode.right != null)\n                q.offer(new Node(current.treeNode.right, current.horizontalDistance + 1));\n        }\n        for(int i = min; i \u003c= max; i++) {\n            res.add(map.get(i));\n        }\n       return res; \n    }\n}\n```\n\n# References :\nhttps://www.youtube.com/watch?v=QWbVCqIhTO4\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fbinary-tree-vertical-order-traversal","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fbinary-tree-vertical-order-traversal","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fbinary-tree-vertical-order-traversal/lists"}