首页 > 其他 > 详细

958. Check Completeness of a Binary Tree

时间:2019-08-29 14:19:07      阅读:68      评论:0      收藏:0      [点我收藏+]
958. Check Completeness of a Binary Tree

//https://leetcode.com/problems/check-completeness-of-a-binary-tree/description/

class TreeNode(var `val`: Int) {
    var left: TreeNode? = null
    var right: TreeNode? = null
}

class Solution {
    fun isCompleteTree(root: TreeNode?): Boolean {
        if (root == null) {
            return false
        }
        val linkedList = LinkedList<TreeNode>()
        linkedList.add(root)
        while (linkedList.peek()!=null) {
            //if popped node is not null, then add its left and right child, no matter if they are null or not
            val cur = linkedList.poll()
            linkedList.offer(cur.left)
            linkedList.offer(cur.right)
        }
        //get right node first
        while (linkedList.isNotEmpty() && linkedList.peek()==null){
            linkedList.poll()
        }
        return linkedList.isEmpty()
    }
}

 

958. Check Completeness of a Binary Tree

原文:https://www.cnblogs.com/johnnyzhao/p/11429381.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!