This repository contains Java solutions for tree-based problems from LeetCode. It is designed for coding interview preparation and to help developers strengthen their Tree and Binary Search Tree (BST) knowledge.
-
Binary Tree Basics
- Traversals (Preorder, Inorder, Postorder, Level-order)
- Maximum Depth / Minimum Depth
- Diameter of Binary Tree
- Path Sum Problems
-
Binary Search Trees (BST)
- Insertion, Deletion, Search
- Validate BST
- Lowest Common Ancestor
- Convert Sorted Array to BST
-
Advanced Tree Problems
- Serialize & Deserialize Binary Tree
- Construct Tree from Traversals
- Flatten Binary Tree to Linked List
- Binary Tree Cameras
- Recover BST
-
Special Trees
- N-ary Trees
- Trie (Prefix Tree)
- Segment Tree
- Fenwick Tree (Binary Indexed Tree)
-
Clone the repo
git clone https://github.com/<your-username>/LeetCode_Trees_Series.git cd LeetCode_Trees_Series
-
Compile and run Java files
javac Solution.java java Solution
-
Each problem is stored in a separate folder with the following format:
<problem_number>_<problem_name>/ βββ Solution.java βββ README.md (explanation + approach)
LeetCode_Trees_Series/
β
βββ Binary_Tree/
β βββ 0104_Maximum_Depth_of_Binary_Tree/
β β βββ Solution.java
β β βββ README.md
β βββ 0110_Balanced_Binary_Tree/
β β βββ Solution.java
β β βββ README.md
β βββ ...
β
βββ Binary_Search_Tree/
β βββ 0098_Validate_BST/
β β βββ Solution.java
β β βββ README.md
β βββ 0700_Search_in_BST/
β β βββ Solution.java
β β βββ README.md
β βββ ...
β
βββ Advanced_Trees/
βββ 0297_Serialize_and_Deserialize_BT/
β βββ Solution.java
β βββ README.md
βββ ...
Each problem folder contains:
-
README.md
- Problem Link
- Intuition
- Approach
- Complexity Analysis (Time & Space)
-
Solution.java
/** * Problem: 104. Maximum Depth of Binary Tree * Link: https://leetcode.com/problems/maximum-depth-of-binary-tree/ * * Time Complexity: O(n) * Space Complexity: O(h) where h is tree height */ class Solution { public int maxDepth(TreeNode root) { if (root == null) return 0; return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); } }
- β Build a complete set of Java tree solutions for LeetCode.
- β Provide clear explanations + clean code.
- β Cover both easy, medium, and hard tree problems.
- β Help students & professionals crack FAANG / top company interviews.
- Fork the repo & add your solution.
- Make sure code is properly formatted and commented.
- Submit a PR π
If you find this repository helpful, please star β it and share with friends preparing for DSA in Java!