Depth First Search

Depth-First Search (DFS) is a graph traversal algorithm used to explore and search through data structures like graphs or trees. Unlike Breadth-First Search (BFS), DFS starts at a given node and explores as far as possible along each branch before backtracking. It’s like traversing down a single path as deeply as possible before exploring other paths.

  • Binary Tree Inorder Traversal
    Given the root of a binary tree, return the inorder traversal of its nodes’ values. In inorder traversal, we visit the left subtree first, then the current node, and finally the right subtree.
  • Binary Tree Maximum Path Sum
    Given a binary tree, find the maximum path sum. The maximum path sum is defined as the maximum sum of any path from any node to any node in the tree. The path may start and end at any node in the tree.
  • Path Sum
    Given a binary tree and a target sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given target sum. A leaf is a node with no children.
  • Path Sum II
    Given a binary tree and a target sum, find all root-to-leaf paths where each path’s sum equals the given target sum. A leaf is a node with no children.
  • Path Sum III
    Given a binary tree and a target sum, find the total number of all paths in the tree such that the sum of the values along the path equals the given target sum. The paths can start and end at any node in the tree, but they must go in the downward direction.
  • Range Sum of BST
    Given the root of a binary search tree (BST), and integers low and high, find the sum of all node values in the BST that are within the range [low, high].
  • Sum Root to Leaf Numbers
    Given a binary tree, where each node has a value between 0 and 9, find the total sum of all root-to-leaf numbers. A root-to-leaf number is a number represented by the concatenation of all node values along the path from the root to that leaf.