返回介绍

solution / 0200-0299 / 0222.Count Complete Tree Nodes / README_EN

发布于 2024-06-17 01:04:03 字数 11536 浏览 0 评论 0 收藏 0

222. Count Complete Tree Nodes

中文文档

Description

Given the root of a complete binary tree, return the number of the nodes in the tree.

According to Wikipedia, 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 1 and 2h nodes inclusive at the last level h.

Design an algorithm that runs in less than O(n) time complexity.

 

Example 1:

Input: root = [1,2,3,4,5,6]
Output: 6

Example 2:

Input: root = []
Output: 0

Example 3:

Input: root = [1]
Output: 1

 

Constraints:

  • The number of nodes in the tree is in the range [0, 5 * 104].
  • 0 <= Node.val <= 5 * 104
  • The tree is guaranteed to be complete.

Solutions

Solution 1: Recursion

We recursively traverse the entire tree and count the number of nodes.

The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of nodes in the tree.

# Definition for a binary tree node.
# class TreeNode:
#   def __init__(self, val=0, left=None, right=None):
#     self.val = val
#     self.left = left
#     self.right = right
class Solution:
  def countNodes(self, root: Optional[TreeNode]) -> int:
    if root is None:
      return 0
    return 1 + self.countNodes(root.left) + self.countNodes(root.right)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *   int val;
 *   TreeNode left;
 *   TreeNode right;
 *   TreeNode() {}
 *   TreeNode(int val) { this.val = val; }
 *   TreeNode(int val, TreeNode left, TreeNode right) {
 *     this.val = val;
 *     this.left = left;
 *     this.right = right;
 *   }
 * }
 */
class Solution {
  public int countNodes(TreeNode root) {
    if (root == null) {
      return 0;
    }
    return 1 + countNodes(root.left) + countNodes(root.right);
  }
}
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *   int val;
 *   TreeNode *left;
 *   TreeNode *right;
 *   TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *   TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *   TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
  int countNodes(TreeNode* root) {
    if (!root) {
      return 0;
    }
    return 1 + countNodes(root->left) + countNodes(root->right);
  }
};
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *   Val int
 *   Left *TreeNode
 *   Right *TreeNode
 * }
 */
func countNodes(root *TreeNode) int {
  if root == nil {
    return 0
  }
  return 1 + countNodes(root.Left) + countNodes(root.Right)
}
use std::cell::RefCell;
use std::rc::Rc;

impl Solution {
  pub fn count_nodes(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
    if let Some(node) = root {
      let node = node.borrow();
      let left = Self::depth(&node.left);
      let right = Self::depth(&node.right);
      if left == right {
        Self::count_nodes(node.right.clone()) + (1 << left)
      } else {
        Self::count_nodes(node.left.clone()) + (1 << right)
      }
    } else {
      0
    }
  }

  fn depth(root: &Option<Rc<RefCell<TreeNode>>>) -> i32 {
    if let Some(node) = root { Self::depth(&node.borrow().left) + 1 } else { 0 }
  }
}
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *   this.val = (val===undefined ? 0 : val)
 *   this.left = (left===undefined ? null : left)
 *   this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var countNodes = function (root) {
  if (!root) {
    return 0;
  }
  return 1 + countNodes(root.left) + countNodes(root.right);
};
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *   public int val;
 *   public TreeNode left;
 *   public TreeNode right;
 *   public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
 *     this.val = val;
 *     this.left = left;
 *     this.right = right;
 *   }
 * }
 */
public class Solution {
  public int CountNodes(TreeNode root) {
    if (root == null) {
      return 0;
    }
    return 1 + CountNodes(root.left) + CountNodes(root.right);
  }
}

Solution 2: Binary Search

For this problem, we can also take advantage of the characteristics of a complete binary tree to design a faster algorithm.

Characteristics of a complete binary tree: leaf nodes can only appear on the bottom and second-to-bottom layers, and the leaf nodes on the bottom layer are concentrated on the left side of the tree. It should be noted that a full binary tree is definitely a complete binary tree, but a complete binary tree is not necessarily a full binary tree.

If the number of layers in a full binary tree is $h$, then the total number of nodes is $2^h - 1$.

We first count the heights of the left and right subtrees of $root$, denoted as $left$ and $right$.

  1. If $left = right$, it means that the left subtree is a full binary tree, so the total number of nodes in the left subtree is $2^{left} - 1$. Plus the $root$ node, it is $2^{left}$. Then we recursively count the right subtree.
  2. If $left > right$, it means that the right subtree is a full binary tree, so the total number of nodes in the right subtree is $2^{right} - 1$. Plus the $root$ node, it is $2^{right}$. Then we recursively count the left subtree.

The time complexity is $O(\log^2 n)$.

# Definition for a binary tree node.
# class TreeNode:
#   def __init__(self, val=0, left=None, right=None):
#     self.val = val
#     self.left = left
#     self.right = right
class Solution:
  def countNodes(self, root: Optional[TreeNode]) -> int:
    def depth(root):
      d = 0
      while root:
        d += 1
        root = root.left
      return d

    if root is None:
      return 0
    left, right = depth(root.left), depth(root.right)
    if left == right:
      return (1 << left) + self.countNodes(root.right)
    return (1 << right) + self.countNodes(root.left)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *   int val;
 *   TreeNode left;
 *   TreeNode right;
 *   TreeNode() {}
 *   TreeNode(int val) { this.val = val; }
 *   TreeNode(int val, TreeNode left, TreeNode right) {
 *     this.val = val;
 *     this.left = left;
 *     this.right = right;
 *   }
 * }
 */
class Solution {
  public int countNodes(TreeNode root) {
    if (root == null) {
      return 0;
    }
    int left = depth(root.left);
    int right = depth(root.right);
    if (left == right) {
      return (1 << left) + countNodes(root.right);
    }
    return (1 << right) + countNodes(root.left);
  }

  private int depth(TreeNode root) {
    int d = 0;
    for (; root != null; root = root.left) {
      ++d;
    }
    return d;
  }
}
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *   int val;
 *   TreeNode *left;
 *   TreeNode *right;
 *   TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *   TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *   TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
  int countNodes(TreeNode* root) {
    if (!root) {
      return 0;
    }
    int left = depth(root->left);
    int right = depth(root->right);
    if (left == right) {
      return (1 << left) + countNodes(root->right);
    }
    return (1 << right) + countNodes(root->left);
  }

  int depth(TreeNode* root) {
    int d = 0;
    for (; root; root = root->left) {
      ++d;
    }
    return d;
  }
};
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *   Val int
 *   Left *TreeNode
 *   Right *TreeNode
 * }
 */
func countNodes(root *TreeNode) int {
  if root == nil {
    return 0
  }
  left, right := depth(root.Left), depth(root.Right)
  if left == right {
    return (1 << left) + countNodes(root.Right)
  }
  return (1 << right) + countNodes(root.Left)
}

func depth(root *TreeNode) (d int) {
  for ; root != nil; root = root.Left {
    d++
  }
  return
}
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *   this.val = (val===undefined ? 0 : val)
 *   this.left = (left===undefined ? null : left)
 *   this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var countNodes = function (root) {
  const depth = root => {
    let d = 0;
    for (; root; root = root.left) {
      ++d;
    }
    return d;
  };
  if (!root) {
    return 0;
  }
  const left = depth(root.left);
  const right = depth(root.right);
  if (left == right) {
    return (1 << left) + countNodes(root.right);
  }
  return (1 << right) + countNodes(root.left);
};
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *   public int val;
 *   public TreeNode left;
 *   public TreeNode right;
 *   public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
 *     this.val = val;
 *     this.left = left;
 *     this.right = right;
 *   }
 * }
 */
public class Solution {
  public int CountNodes(TreeNode root) {
    if (root == null) {
      return 0;
    }
    int left = depth(root.left);
    int right = depth(root.right);
    if (left == right) {
      return (1 << left) + CountNodes(root.right);
    }
    return (1 << right) + CountNodes(root.left);
  }

  private int depth(TreeNode root) {
    int d = 0;
    for (; root != null; root = root.left) {
      ++d;
    }
    return d;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文