Last active
May 16, 2019 21:53
-
-
Save Mintri1199/1e3b91171779aeae65a649f8ea487f07 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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