GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • LeetCodeJavascript.com
  • Star on GitHub

This view

  • Original page
  • New address

GeezerVison is showing leetcodejavascript.com — #958 - Check Completeness of a Binary Tree - LeetCode JavaScript Solutions

Back to all solutions

#958 - Check Completeness of a Binary Tree

Medium • View on LeetCode • View on GitHub
Tree Breadth-First Search Binary Tree

Problem 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.

Solution

/**
 * 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 {boolean}
 */
var isCompleteTree = function(root) {
  const queue = [root];
  let foundNull = false;

  while (queue.length) {
    const node = queue.shift();
    if (!node) {
      foundNull = true;
      continue;
    }
    if (foundNull) return false;
    queue.push(node.left);
    queue.push(node.right);
  }

  return true;
};
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.