Leetcode - Partition Labels
Leetcode - Valid Palindrome

Leetcode - Count Complete Tree Nodes

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

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

Your violets blog is so amazing readers. [url="https://www.ishadelhi.in]Hot Delhi Escorts[/url] Great job.

10th Exam Pattern 20 说:
Feb 14, 2023 03:01:23 PM

If you are concerned about the 10th Blueprint 2024 and want to check the 10th Exam Pattern 2024, you can go to the official website and obtain the most recent information. The 10th Exam Pattern 2024 announcement will be made available on the official website. 10th Exam Pattern 2024 If there are any more places where you can acquire the 10th Exam Pattern 2024, we will update this post. We have now provided some internet links where you may obtain your 10th Exam Pattern 2024 Board Online.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter