简介

思路: 个数和序号相等

code

class Solution {
    int size = 0;
    int maxCode = 0;
    public boolean isCompleteTree(TreeNode root) {
        if(root == null) return true;
        recursive(root, 1);
        return size == maxCode;
    }
    public void recursive(TreeNode root, int index) {
        if(root == null) return;
        size ++;
        maxCode = Math.max(maxCode, index);
        recursive(root.left, index * 2);
        recursive(root.right, index * 2 + 1);
    }
}
posted on 2021-06-09 16:32  HDU李少帅  阅读(21)  评论(0编辑  收藏  举报