返回介绍

solution / 0900-0999 / 0958.Check Completeness of a Binary Tree / README_EN

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

958. Check Completeness of a Binary Tree

中文文档

Description

Given the root of a binary tree, determine if it is a _complete binary tree_.

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 1:

Input: root = [1,2,3,4,5,6]
Output: true
Explanation: Every level before the last is full (ie. levels with node-values {1} and {2, 3}), and all nodes in the last level ({4, 5, 6}) are as far left as possible.

Example 2:

Input: root = [1,2,3,4,5,null,7]
Output: false
Explanation: The node with value 7 isn't as far left as possible.

 

Constraints:

  • The number of nodes in the tree is in the range [1, 100].
  • 1 <= Node.val <= 1000

Solutions

Solution 1

# 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 isCompleteTree(self, root: TreeNode) -> bool:
    q = deque([root])
    while q:
      node = q.popleft()
      if node is None:
        break
      q.append(node.left)
      q.append(node.right)
    return all(node is None for node in q)
/**
 * 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 boolean isCompleteTree(TreeNode root) {
    Deque<TreeNode> q = new LinkedList<>();
    q.offer(root);
    while (q.peek() != null) {
      TreeNode node = q.poll();
      q.offer(node.left);
      q.offer(node.right);
    }
    while (!q.isEmpty() && q.peek() == null) {
      q.poll();
    }
    return q.isEmpty();
  }
}
/**
 * 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:
  bool isCompleteTree(TreeNode* root) {
    queue<TreeNode*> q{{root}};
    while (q.front()) {
      root = q.front();
      q.pop();
      q.push(root->left);
      q.push(root->right);
    }
    while (!q.empty() && !q.front()) q.pop();
    return q.empty();
  }
};
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *   Val int
 *   Left *TreeNode
 *   Right *TreeNode
 * }
 */
func isCompleteTree(root *TreeNode) bool {
  q := []*TreeNode{root}
  for q[0] != nil {
    root = q[0]
    q = q[1:]
    q = append(q, root.Left)
    q = append(q, root.Right)
  }
  for len(q) > 0 && q[0] == nil {
    q = q[1:]
  }
  return len(q) == 0
}

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

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

发布评论

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