金融AIGC研究
优质  高效     
优质的技术交付服务 迅捷的办事效率
我们专注品质与服务   决胜制高点  细节决定成败
Runoff commanding heights Detail decides success or failure
The commanding heights of
the details determine success or failure
技术动态
DETAIL
Leetcode 学习第三天
来源:互联网 | 作者:business-101 | 发布时间: 2024-03-23 | 286 次浏览 | 分享到:

https://datawhalechina.github.io/leetcode-notes/#/ch07/07.03/07.03.01-Exercises

检查完全二叉树,碰到了点坑,

硬要求就是要节点要尽量往前排,不允许上层没排满就排下层


还是要靠递归来解决

以下链接在电脑端打开

(https://www.odc.org.cn/page148?article_id=61)


1.0958. 二叉树的完全性检验


/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean child(List<TreeNode> al,boolean flag){
        boolean ret=true;
        if (flag){
            ret=true;
            for(TreeNode ax:al)ret=ret&&ax==null;
            return ret;
        }
        ret=true;
        List<TreeNode> cc=new ArrayList<>();
        for(TreeNode ax:al)if(ax!=null&&ret){cc.add(ax.left);cc.add(ax.right);}
        else if(ax!=null&&ret==false)return false;
        else if(ax==null&&ret)ret=false;
        return child(cc,ret==false);
    }
    public boolean isCompleteTree(TreeNode root) {
        List<TreeNode> al=new ArrayList<>();
        al.add(root.left);
        al.add(root.right);
        return child(al,false);
    }
}