LESSON 54 / 75·2 min readIntermediate

Trees and BST in C#

Should knowConcept lesson
01 Understand02 Recall03 Prepare

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);
}

PAUSE & RECALL

Can you explain it without the notes?

Trees and BST in C#

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: Heap and priority queue in .NET

Adapted from 17_DSA_DOTNET.md · “7. Trees and BST in C#”. Examples are study fragments; framework behavior can depend on your version.