68 lines
1.2 KiB
Go
68 lines
1.2 KiB
Go
package main
|
|
import (
|
|
"fmt"
|
|
"bufio"
|
|
"strconv"
|
|
"strings"
|
|
"os"
|
|
)
|
|
|
|
func main() {
|
|
var n int
|
|
var tree *node
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
for scanner.Scan() {
|
|
text := scanner.Text()
|
|
if n == 0 {
|
|
n, _ = strconv.Atoi(text)
|
|
continue
|
|
}
|
|
for _, s := range strings.Fields(text) {
|
|
if v, err := strconv.Atoi(s); err == nil {
|
|
tree = insert(tree, &node{value:v})
|
|
}
|
|
}
|
|
}
|
|
fmt.Println(height(tree, 0))
|
|
}
|
|
|
|
type node struct{
|
|
value int
|
|
left *node
|
|
right *node
|
|
}
|
|
|
|
func insert(root, n *node) *node {
|
|
if root == nil {
|
|
return n
|
|
}
|
|
if n.value < root.value {
|
|
root.left = insert(root.left, n)
|
|
return root
|
|
}
|
|
root.right = insert(root.right, n)
|
|
return root
|
|
}
|
|
|
|
func height(root *node, currentHeight int) int {
|
|
if root == nil {
|
|
return -1
|
|
}
|
|
|
|
maxHeight := currentHeight
|
|
|
|
if root.left != nil {
|
|
if nh := height(root.left, currentHeight+1); nh > maxHeight {
|
|
maxHeight = nh
|
|
}
|
|
}
|
|
|
|
if root.right != nil {
|
|
if nh := height(root.right, currentHeight+1); nh > maxHeight {
|
|
maxHeight = nh
|
|
}
|
|
}
|
|
|
|
return maxHeight
|
|
}
|