{"id":22513935,"url":"https://github.com/emahtab/cousins-in-binary-tree","last_synced_at":"2026-01-07T06:18:12.174Z","repository":{"id":79525399,"uuid":"237725249","full_name":"eMahtab/cousins-in-binary-tree","owner":"eMahtab","description":"Check whether given two nodes are cousins in a binary tree","archived":false,"fork":false,"pushed_at":"2020-05-29T17:34:40.000Z","size":4,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-02T03:26:10.534Z","etag":null,"topics":["binary-tree","dfs","leetcode","problem-solving"],"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-02-02T05:47:39.000Z","updated_at":"2020-05-29T17:34:42.000Z","dependencies_parsed_at":"2023-05-10T17:15:45.390Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/cousins-in-binary-tree","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%2Fcousins-in-binary-tree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcousins-in-binary-tree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcousins-in-binary-tree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcousins-in-binary-tree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/cousins-in-binary-tree/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245952222,"owners_count":20699449,"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","dfs","leetcode","problem-solving"],"created_at":"2024-12-07T03:15:06.976Z","updated_at":"2026-01-07T06:18:07.137Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Cousins in Binary Tree\n## https://leetcode.com/problems/cousins-in-binary-tree\n\nIn a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1.\n\nTwo nodes of a binary tree are cousins if they have the same depth, but have different parents.\n\nWe are given the root of a binary tree with unique values, and the values x and y of two different nodes in the tree.\n\nReturn true if and only if the nodes corresponding to the values x and y are cousins.\n\n\n## Implementation :\n\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    Map\u003cInteger,Integer\u003e parentMap = new HashMap\u003c\u003e();\n    Map\u003cInteger,Integer\u003e depthMap = new HashMap\u003c\u003e();\n    \n    public boolean isCousins(TreeNode root, int x, int y) {\n        if(root == null)\n            return false;\n        dfs(root, null, 0);\n        return (parentMap.get(x) != parentMap.get(y)) \u0026\u0026 (depthMap.get(x) == depthMap.get(y));\n    }\n    \n    private void dfs(TreeNode node, TreeNode parent, int depth) {\n        parentMap.put(node.val, parent == null ? null : parent.val);\n        depthMap.put(node.val, depth);\n        if(node.left != null)\n            dfs(node.left, node, depth+1);\n        if(node.right != null)\n            dfs(node.right, node, depth+1);\n    }\n}\n```\n\n## Implementation 2:\n```java\nclass Solution {\n    public boolean isCousins(TreeNode root, int x, int y) {\n        if(root == null)\n            return false;\n        Map\u003cInteger, Integer\u003e depthMap = new HashMap\u003c\u003e();\n        Map\u003cInteger, Integer\u003e parentMap = new HashMap\u003c\u003e();\n        depthMap.put(root.val, 0); parentMap.put(root.val, Integer.MIN_VALUE);\n        traverseTree(root, depthMap, parentMap, 0);\n        return depthMap.get(x) == depthMap.get(y) \u0026\u0026 parentMap.get(x) != parentMap.get(y);\n    }\n    \n    private void traverseTree(TreeNode node, Map\u003cInteger, Integer\u003e depthMap, \n                              Map\u003cInteger, Integer\u003e parentMap, int depth) {\n        if(node.left != null) {\n            depthMap.put(node.left.val, depth+1);\n            parentMap.put(node.left.val, node.val);\n            traverseTree(node.left, depthMap, parentMap, depth+1);\n        }\n        if(node.right != null) {\n            depthMap.put(node.right.val, depth+1);\n            parentMap.put(node.right.val, node.val);\n            traverseTree(node.right, depthMap, parentMap, depth+1);\n        }\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fcousins-in-binary-tree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fcousins-in-binary-tree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fcousins-in-binary-tree/lists"}