Untitled

Run Settings
LanguageGo
Language Version
Run Command
// This is the text editor interface. // Anything you type or change here will be seen by the other person in real time. // 5 // 3 7 // 1 4 6 10 // 8 // n := getFloor(target int) // 3 -> Node 3 // 2 -> Node 1 // 9 -> Node 7 // 0 -> nil package main import "fmt" type BNode struct { Value int Parent *BNode Left *BNode Right *BNode } func getFloor(root *BNode, target int) *BNode { if root.Value == target { return root; } var prev *BNode if root.Value < target { prev = root if root.Right == nil { return prev } return getFloor(root.Right, target) } else if root.Value > target { if root.Left != nil { return getFloor(root.Left, target) } } return nil } func test(node *BNode, target int, expected *int) { res := getFloor(node, target) if res == nil { if expected == nil { fmt.Printf("PASS\n") } else { e := *expected fmt.Printf("Expected %d, got nil\n", e) } } else if res != nil { if expected == nil { fmt.Printf("Expected nil, got %d\n", res.Value) return } e := *expected if res.Value == e { fmt.Printf("PASS\n") } else { fmt.Printf("GOT %d, Expected %d\n", res.Value, e) } } } func Int(v int) *int{ return &v } func main() { root := BNode{Value: 5, Left: &BNode{Value: 3, Left: &BNode{Value: 1}, Right: &BNode{Value: 4}, }, Right: &BNode{Value: 7, Left: &BNode{Value: 6}, Right: &BNode{Value: 10, Left: &BNode{Value: 8}, }, }, } test(&root, -1, nil) test(&root, 0, nil) test(&root, 9, Int(8)) test(&root, 1, Int(1)) test(&root, 5, Int(5)) test(&root, 2, Int(1)) test(&root, 10, Int(10)) test(&root, 100, Int(10)) }
Editor Settings
Theme
Key bindings
Full width
Lines