Simple BST with values 1,2,3 — print inorder sum.
| Test | Status | Details |
|---|
Ready — edit the code above and click Run or Submit.
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.
Inorder DFS on binary tree.