LESSON 40 / 75·2 min readIntermediate

Trees and BST in Java

Should knowConcept lesson
01 Understand02 Recall03 Prepare

Tree node

java
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int val) { this.val = val; }
}

Problems to practice

  • Inorder traversal
  • Preorder traversal
  • Postorder traversal
  • Level order traversal
  • Maximum depth
  • Diameter of tree
  • LCA
  • Validate BST
  • Path Sum

Example: DFS traversal

java
void inorder(TreeNode root) {
    if (root == null) return;
    inorder(root.left);
    System.out.print(root.val + " ");
    inorder(root.right);
}

Interview guidance

BST property helps with search and sorted traversal.


PAUSE & RECALL

Can you explain it without the notes?

Trees and BST in Java

Use your own words. Explain an example and one limitation before checking the explanation.

Check your explanation

Tree node

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: Heaps and priority queue in Java

Adapted from 15_DSA_JAVA.md · “7. Trees and BST in Java”. Examples are study fragments; framework behavior can depend on your version.