码迷,mamicode.com
首页 > 其他好文 > 详细

判断二叉树是否是完全二叉树

时间:2018-07-10 11:17:50      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:nbsp   add   style   col   ISE   OLE   asn   linked   res   

boolean isCompleteTreeNode(TreeNode root){
        if(root == null){
            return false;
        }
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.add(root);
        boolean result = true;
        boolean hasNoChild = false;
        while(!queue.isEmpty()){
            TreeNode current = queue.remove();
            if(hasNoChild){
                if(current.left!=null||current.right!=null){
                    result = false;
                    break;
                }
            }else{
                if(current.left!=null&&current.right!=null){
                    queue.add(current.left);
                    queue.add(current.right);
                }else if(current.left!=null&&current.right==null){
                    queue.add(current.left);
                    hasNoChild = true;
                    
                }else if(current.left==null&&current.right!=null){
                    result = false;
                    break;
                }else{
                    hasNoChild = true;
                }
            }
            
        }
        return result;
    }

 

判断二叉树是否是完全二叉树

标签:nbsp   add   style   col   ISE   OLE   asn   linked   res   

原文地址:https://www.cnblogs.com/yingpu/p/9287190.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!