Intermediate·Concept·2 min read

Trees and BST in C#

Practice this question

Tree node

csharp
public class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;
    public TreeNode(int val = 0, TreeNode left = null, TreeNode right = null) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

Problems to practice

  • Inorder traversal
  • Preorder traversal
  • Postorder traversal
  • Level order traversal
  • Maximum depth
  • Diameter of tree
  • Lowest common ancestor
  • Validate BST
  • Path sum

Example: inorder traversal

csharp
public void Inorder(TreeNode root) {
    if (root == null) return;
    Inorder(root.left);
    Console.Write(root.val + " ");
    Inorder(root.right);
}

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