-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem357c.go
46 lines (36 loc) · 875 Bytes
/
problem357c.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
You are given a binary tree in a peculiar string representation. Each node is
written in the form (lr), where l corresponds to the left child and r
corresponds to the right child.
If either l or r is null, it will be represented as a zero. Otherwise, it will
be represented by a new (lr) pair.
Here are a few examples:
A root node with no children: (00)
A root node with two children: ((00)(00))
An unbalanced tree with three consecutive left children: ((((00)0)0)0)
Given this representation, determine the depth of the tree.
*/
package main
import (
"fmt"
"os"
)
func main() {
stringrep := []rune(os.Args[1])
count := 0
maxcount := 0
for _, r := range stringrep {
switch r {
case '(':
count++
if count > maxcount {
maxcount = count
}
case ')':
count--
case '0':
default:
}
}
fmt.Printf("Depth %d\n", maxcount)
}