Skip to content

Latest commit

Β 

History

History
172 lines (138 loc) Β· 13.9 KB

File metadata and controls

172 lines (138 loc) Β· 13.9 KB

Binary Tree

Welcome to the Binary Tree section of Javarena! This guide covers binary tree concepts, common operations, and interview problems with Java implementations to help you ace coding interviews.


πŸ“Œ Table of Contents


🌟 What is a Binary Tree?

A binary tree is a hierarchical data structure where each node has at most two children, referred to as the left child and right child. Binary trees are widely used in search algorithms, expression parsing, and hierarchical data representation.

Visual Representation

1
/
2   3
/ \

4   5   6 
  • Root: Node 1 (topmost node)
  • Parent: A node with children (e.g., 2 is the parent of 4 and 5)
  • Leaf: Nodes with no children (e.g., 4, 5, 6)

🌲 Types of Binary Trees

  • Full Binary Tree: Every node has either 0 or 2 children.
  • Complete Binary Tree: All levels except possibly the last are fully filled, and the last level is filled from left to right.
  • Perfect Binary Tree: All internal nodes have 2 children, and all leaf nodes are at the same level.
  • Binary Search Tree (BST): Left subtree contains nodes with values less than the parent, and right subtree contains nodes with values greater than the parent.
  • Balanced Binary Tree: Height of left and right subtrees of any node differs by at most 1 (e.g., AVL, Red-Black trees).

πŸ“ Key Properties

  • Height: Number of edges on the longest path from root to leaf.
  • Depth: Number of edges from the root to a specific node.
  • Maximum nodes at level L: 2^L nodes.
  • Maximum nodes in a tree of height H: 2^(H+1) - 1.
  • Minimum height for N nodes: floor(log2(N)).

βš™οΈ Common Operations

Operation Time Complexity Description
Inorder Traversal O(n) Left β†’ Root β†’ Right
Preorder Traversal O(n) Root β†’ Left β†’ Right
Postorder Traversal O(n) Left β†’ Right β†’ Root
Level Order Traversal O(n) Traverse level by level
Insertion (BST) O(h) (h = height) Insert a node in BST
Deletion (BST) O(h) Remove a node from BST
Search (BST) O(h) Find a node in BST

πŸ’» Code Implementations

Below is a Java implementation of a binary tree node and inorder traversal:

public class BinaryTree {
    // Node class for binary tree
    static class Node {
        int val;
        Node left;
        Node right;

        Node(int val) {
            this.val = val;
            this.left = null;
            this.right = null;
        }
    }

    // Inorder traversal: Left -> Root -> Right
    public void inorderTraversal(Node root) {
        if (root == null) return;
        inorderTraversal(root.left);
        System.out.print(root.val + " ");
        inorderTraversal(root.right);
    }

    public static void main(String[] args) {
        BinaryTree tree = new BinaryTree();
        Node root = new Node(1);
        root.left = new Node(2);
        root.right = new Node(3);
        root.left.left = new Node(4);
        root.left.right = new Node(5);
        root.right.right = new Node(6);

        System.out.print("Inorder Traversal: ");
        tree.inorderTraversal(root); // Output: 4 2 5 1 3 6
    }
}

πŸ§ͺ Practice Problems

# Problem Solution File
1 Preorder Traversal BinaryTree.java
2 Inorder Traversal BinaryTree.java
3 Postorder Traversal BinaryTree.java
4 Level Order Traversal BinaryTree.java
5 Preorder, Postorder and Inorder Traversal of a Binary Tree using a single Stack BinaryTree.java
6 Identical Trees Solutions.java
7 Symmetric Tree Solutions.java
8 Maximum Depth of Binary Tree Solutions.java
9 Binary Tree Zigzag Level Order Traversal Solutions.java
10 Validate Binary Search Tree Solutions.java
11 Same Tree Solutions.java
12 Balanced Binary Tree Solutions.java
13 Binary Tree Maximum Path Sum Solutions.java
14 Vertical Order Traversal of a Binary Tree Traversals.java
15 Tree Boundary Traversal Traversals.java
16 Top View of Binary Tree Traversals.java
17 Bottom View of Binary Tree Traversals.java
18 Binary Tree Right Side View Traversals.java
19 Root Equals Sum of Children Solutions.java
20 Binary Tree Paths Solutions.java
21 LCA of Binary Tree LCA.java
22 Maximum Width of Binary Tree Solutions.java
23 All Nodes Distance K in Binary Tree Solutions.java
24 Amount of Time for Binary Tree to Be Infected
25 Count Complete Tree Nodes CountNodes.java
26 Construct Binary Tree from Preorder and Inorder Traversal ConstructTree.java
27 Construct Binary Tree from Inorder and Postorder Traversal ConstructTree.java
28 BST Search BSTSearch.java
29 Closest Nodes Queries in a Binary Search Tree BSTClosetNode.java
30 Even Odd Tree EvenOddTree.java
31 Diameter Of Binary Tree DiameterOfBinaryTree.java
32 Cousins in Binary Tree CousinsInBinaryTree.java
33 House Robber III HouseRobberIII.java
34 Invert Binary Tree InvertTree.java
35 Serialize and Deserialize Binary Tree SerializeDeserialize.java
36 Subtree of Another Tree SubTree.java
37 Kth Smallest Ele KthSmallest.java

πŸ“š Resources