java.lang.Object
g0201_0300.s0222_count_complete_tree_nodes.Solution

public class Solution extends Object
222 - Count Complete Tree Nodes.<p>Medium</p> <p>Given the <code>root</code> of a <strong>complete</strong> binary tree, return the number of the nodes in the tree.</p> <p>According to <strong><a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_top">Wikipedia</a></strong> , every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between <code>1</code> and <code>2<sup>h</sup></code> nodes inclusive at the last level <code>h</code>.</p> <p>Design an algorithm that runs in less than <code>O(n)</code> time complexity.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/01/14/complete.jpg" alt="" /></p> <p><strong>Input:</strong> root = [1,2,3,4,5,6]</p> <p><strong>Output:</strong> 6</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> root = []</p> <p><strong>Output:</strong> 0</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> root = [1]</p> <p><strong>Output:</strong> 1</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li> <li><code>0 <= Node.val <= 5 * 10<sup>4</sup></code></li> <li>The tree is guaranteed to be <strong>complete</strong>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • countNodes

      public int countNodes(TreeNode root)