Leetcode - Count Complete Tree Nodes

violet posted @ Jun 24, 2020 05:34:59 AM in 算法 with tags Algorithm DFS Golang tree , 405 阅读

https://leetcode.com/problems/count-complete-tree-nodes/

Given a complete binary tree, count the number of nodes.

Note:

Definition of a complete binary tree from Wikipedia:
In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.

Example:

```Input:
1
/ \
2   3
/ \  /
4  5 6

Output: 6```

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

func find(node *TreeNode) int {
if node == nil {
return 0
}

return 1 + find(node.Left) + find(node.Right)
}```
Anamika SHukla 说:
May 31, 2021 04:30:45 AM

All the mathematical solutions are provided here.

Bangalore Escorts@gm 说:
Nov 02, 2021 03:08:19 PM

A commitment of appreciation is all together for your sharing. The suspicion you'll contribute extra quality presents on the current page. Much appreciation to you!

Bhawna 说:
Feb 14, 2022 03:25:06 PM