Skip to content

Latest commit

 

History

History
30 lines (26 loc) · 654 Bytes

222.Count_Complete_Tree_Nodes.md

File metadata and controls

30 lines (26 loc) · 654 Bytes

Travesal Tree, if node not equal nil count + 1, else return count. can using recursion or iteration to solution.

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func countNodes(root *TreeNode) int {
	return TraversalTree(root, 0)
}

func TraversalTree(cur *TreeNode, count int) int {
	if cur == nil {
		return count
	}
	count++
	count = TraversalTree(cur.Left, count)
	count = TraversalTree(cur.Right, count)
	return count
}