Skip to content
Learn Netverks

Lesson

Step 24/36 67% through track

binary-trees

Binary trees

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Binary trees: data structure and algorithm concepts with complexity analysis and interview-ready C++ examples.

Hierarchical and priority data drives search, scheduling, and streaming aggregates.

You will apply Binary trees in contexts like: File systems, DOM-like hierarchies, schedulers, and top-K dashboards.

Compile and run C++17 snippets in the playground (`int main`, `std::cout`); after each run, state time and space complexity before moving on.

When you can explain the previous lesson's ideas in your own words.

A binary tree node has at most two children (left, right). Binary search trees order keys so left < root < right—enabling O(log n) search when balanced.

Terminology

  • Root — top node
  • Leaf — no children
  • Height — longest path to leaf
  • Full binary tree — each node 0 or 2 children

Node struct

#include 

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
};

int main() {
    TreeNode root{2, nullptr, nullptr};
    TreeNode left{1, nullptr, nullptr};
    TreeNode right{3, nullptr, nullptr};
    root.left = &left;
    root.right = &right;
    std::cout << "root=" << root.val << " left=" << root.left->val << "\n";
    return 0;
}

Balanced vs skewed

Skewed tree height n → operations degrade to O(n). AVL/red-black trees rebalance—STL std::map uses balanced tree internally.

Important interview questions and answers

  1. Q: Binary tree vs BST?
    A: BST property on values; generic binary tree only caps children at two.
  2. Q: Max nodes at height h?
    A: Up to 2^h leaves in a full level—guides recursion depth estimates.

Self-check

  1. Define root, leaf, and height.
  2. When does BST search become O(n)?

Pitfall: Skewed BST height n degrades to linked-list performance—mention balanced trees.

Interview prep

BST property?

Left subtree smaller, right larger than node.

Skewed tree height?

O(n)—degrades search to linear.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • BST property?
  • Height vs nodes?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump