...

/

Solution Review: Number of Full Nodes in a Binary Tree

Solution Review: Number of Full Nodes in a Binary Tree

Let’s take a detailed look at the previous challenge’s solution.

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 main
import "fmt"
func (t *Tree) NumFullNodesBT() int {
return numFullNodesBT(t.root)
}
func numFullNodesBT(curr *Node) int {
var count int
if 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())
}

Time complexity

...