zoukankan      html  css  js  c++  java
  • 958. Check Completeness of a Binary Tree

    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()
        }
    }
  • 相关阅读:
    MYSQL之基本操作
    Python操作Mysql之基本操作
    编辑器
    iOS项目评估报告
    mac安装as配置
    屏幕适配
    CocoaPods配置步骤
    android网络监测
    获取通讯录
    json解析
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/11429381.html
Copyright © 2011-2022 走看看