Solution Review: Number of Full Nodes in a Binary Tree
Let’s take a detailed look at the previous challenge’s solution.
We'll cover the following...
We'll cover the following...
Solution
A full node is a node that has both left and right children. We’ll recursively traverse the whole tree and will increase the count of full nodes as we find them.
Code
Go (1.6.2)
Files
package mainimport "fmt"func (t *Tree) NumFullNodesBT() int {return numFullNodesBT(t.root)}func numFullNodesBT(curr *Node) int {var count intif curr == nil {return 0}count = numFullNodesBT(curr.right) + numFullNodesBT(curr.left)if curr.right != nil && curr.left != nil {count++}return count}/* Testing Code */func main() {arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}t := LevelOrderBinaryTree(arr)fmt.Println(t.NumFullNodesBT())}