Skip to content

Instantly share code, notes, and snippets.

@Mintri1199
Last active May 16, 2019 21:53
Show Gist options
  • Save Mintri1199/1e3b91171779aeae65a649f8ea487f07 to your computer and use it in GitHub Desktop.
Save Mintri1199/1e3b91171779aeae65a649f8ea487f07 to your computer and use it in GitHub Desktop.
class DecimalTreeNode {
...
func height() -> Int{
/*Return the height of the node (the number of edges on the longest
downward path from this node to a descendant leaf node).*/
// Early break
if self.isLeaf(){
return 0
}
// Create a array of int to keep track of the height of each branch
var tenPath = Array<Int>(repeating: 0, count: 10)
// Traverse each child node
for path in 0 ... 10 {
// Check if there is a node to begin with
if let newPath = self.next[path]{
tenPath[path] += newPath.height()
}
}
// The program have travese all of it children
return tenPath.max()! + 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment