{"id":22513973,"url":"https://github.com/emahtab/binary-tree-pruning","last_synced_at":"2026-01-30T08:02:33.194Z","repository":{"id":79525317,"uuid":"237768370","full_name":"eMahtab/binary-tree-pruning","owner":"eMahtab","description":"Binary Tree Pruning","archived":false,"fork":false,"pushed_at":"2020-03-11T17:55:24.000Z","size":24,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-14T07:12:48.040Z","etag":null,"topics":["binary-tree","leetcode","problem-solving","pruning"],"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-02T12:33:10.000Z","updated_at":"2020-03-11T17:55:26.000Z","dependencies_parsed_at":"2023-05-10T17:00:40.238Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/binary-tree-pruning","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eMahtab/binary-tree-pruning","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbinary-tree-pruning","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbinary-tree-pruning/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbinary-tree-pruning/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbinary-tree-pruning/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/binary-tree-pruning/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fbinary-tree-pruning/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28908827,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-30T06:42:00.998Z","status":"ssl_error","status_checked_at":"2026-01-30T06:41:58.659Z","response_time":66,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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","leetcode","problem-solving","pruning"],"created_at":"2024-12-07T03:15:18.754Z","updated_at":"2026-01-30T08:02:33.159Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Binary Tree Pruning\n## https://leetcode.com/problems/binary-tree-pruning\n\nWe are given the head node root of a binary tree, where additionally every node's value is either a 0 or a 1.\n\nReturn the same tree where every subtree (of the given tree) not containing a 1 has been removed.\n\n(Recall that the subtree of a node X is X, plus every node that is a descendant of X.)\n\n\n**Note:**\n\n1. The binary tree will have at most 100 nodes.\n2. The value of each node will only be 0 or 1.\n\n![Binary Tree Pruning](binary-tree-pruning.PNG?raw=true \"Binary Tree Pruning\")\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(int x) { val = x; }\n * }\n */\nclass Solution {\n    public TreeNode pruneTree(TreeNode root) {\n        if(root == null)\n            return null;\n        return prune(root);\n    }\n    \n    public TreeNode prune(TreeNode node){\n        if(node == null)\n            return node;\n        node.left = prune(node.left);\n        node.right = prune(node.right);\n        return (node.left == null \u0026\u0026 node.right == null \u0026\u0026 node.val == 0) ?\n                null : node;\n    }\n}\n\n```\n\n# Key Points :\nNote that in the recursive prune method we are first calling the prune on left subtree and right subtree, if we move the check `(node.val == 0 \u0026\u0026 node.left == null \u0026\u0026 node.right == null)` before calling prune on left subtree and right subtree, it will lead to wrong result.\n\n# References :\nhttps://www.youtube.com/watch?v=77LJc56bwnE\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fbinary-tree-pruning","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fbinary-tree-pruning","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fbinary-tree-pruning/lists"}