Skip to content

Commit

Permalink
Create 0958-check-completeness-of-a-binary-tree.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
a93a authored Mar 15, 2023
1 parent 2828145 commit d4a80d8
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions kotlin/0958-check-completeness-of-a-binary-tree.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Example:
* var ti = TreeNode(5)
* var v = ti.`val`
* Definition for a binary tree node.
* class TreeNode(var `val`: Int) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/
class Solution {
fun isCompleteTree(root: TreeNode?): Boolean {

val q = LinkedList<TreeNode?>().apply{
this.addFirst(root)
}

while(q.isNotEmpty()) {
val node = q.removeLast()

if(node != null) {
q.addFirst(node.left)
q.addFirst(node.right)
} else {
while(q.isNotEmpty()) {
if(q.removeLast() != null)
return false
}
}
}

return true
}
}

0 comments on commit d4a80d8

Please sign in to comment.