Intermediate·Concept·2 min read

Trees and BST in Java

Practice this question

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.


Adapted from 15_DSA_JAVA.md · “7. Trees and BST in Java”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.