Medium csharp

BST — inorder traversal sum #19

Problem

Simple BST with values 1,2,3 — print inorder sum.

Hints
  • Left, visit, right

Your solution

TestStatusDetails
Ready — edit the code above and click Run or Submit.

Solution

using System;
using System.Collections.Generic;

class Node { public int Val; public Node L, R; public Node(int v) => Val = v; }
class Program {
    static void Main() {
        var root = new Node(2) { L = new Node(1), R = new Node(3) };
        int sum = 0;
        void Dfs(Node n) { if (n == null) return; Dfs(n.L); sum += n.Val; Dfs(n.R); }
        Dfs(root);
        Console.WriteLine(sum);
    }
}

Try solving on your own first, then reveal the official answer.

Explanation

Inorder DFS on binary tree.

Discussion

0

Sign in to join the discussion.

No discussions yet — ask the first question!

Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details