- Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path0958-check-completeness-of-a-binary-tree.rb
34 lines (26 loc) · 953 Bytes
/
0958-check-completeness-of-a-binary-tree.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# frozen_string_literal: true
# 958. Check Completeness of a Binary Tree
# https://leetcode.com/problems/check-completeness-of-a-binary-tree
=begin
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.
### Constraints:
* The number of nodes in the tree is in the range [1, 100].
* 1 <= Node.val <= 1000
=end
# Definition for a binary tree node.
# class TreeNode
# attr_accessor :val, :left, :right
# def initialize(val = 0, left = nil, right = nil)
# @val = val
# @left = left
# @right = right
# end
# end
# @param {TreeNode} root
# @return {Boolean}
defis_complete_tree(root)
q=[root]
q.pushroot.left,root.rightwhileroot=q.shift
q.count(nil) == q.size
end