Files
LeetCode-Go/leetcode/0331.Verify-Preorder-Serialization-of-a-Binary-Tree/331. Verify Preorder Serialization of a Binary Tree.go
2020-08-07 17:06:53 +08:00

18 lines
268 B
Go

package leetcode
import "strings"
func isValidSerialization(preorder string) bool {
nodes, diff := strings.Split(preorder, ","), 1
for _, node := range nodes {
diff--
if diff < 0 {
return false
}
if node != "#" {
diff += 2
}
}
return diff == 0
}