Intermediate·Concept·2 min read

Trees and BST in JavaScript

Practice this question

Tree node

JavaScript
class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

Problems to practice

  • Inorder, preorder, postorder
  • Level Order Traversal
  • Maximum Depth
  • Diameter of Tree
  • Lowest Common Ancestor
  • Validate BST
  • Path Sum

Example: DFS

JavaScript
function inorder(root) {
  if (!root) return [];
  return [...inorder(root.left), root.val, ...inorder(root.right)];
}

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