代码随想录第16天 | 104. 二叉树的最大深度、595. N叉树的最大深度、111.二叉树的最小深度、222. 完全二叉树的节点个数

文章介绍了如何计算二叉树和N叉树的最大深度、最小深度以及完全二叉树的节点个数。通过递归遍历和层序遍历两种方法,展示了如何确定树的深度,并提供了具体的代码实现。对于最小深度,特别考虑了只有一侧子树的情况。在完全二叉树节点个数的计算中,利用了其特性进行递归或层次遍历计数。
摘要由CSDN通过智能技术生成

104. 二叉树的最大深度

根节点的高度就是二叉树的最大深度
后序遍历计算树的高度

  • 二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
  • 二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数或者节点数(取决于高度从0开始还是从1开始)

递归遍历
递归的单层逻辑:先求它的左子树的深度,再求右子树的深度,最后取左右深度最大的数值 再+1 (加1是因为算上当前中间节点)就是目前节点为根节点的树的深度。

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var maxDepth = function (root) {
    const getDepth = function (node) {
        if (node === null) return 0;
        let leftDepth = getDepth(node.left);
        let rightDepth = getDepth(node.right);
        let depth = 1 + Math.max(leftDepth, rightDepth);
        return depth;
    }
    return getDepth(root);
};

层序遍历

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var maxDepth = function (root) {
    let queue = [], res = 0;
    queue.push(root);
    while (root && queue.length) {
        let length = queue.length;
        while (length--) {
            let node = queue.shift();
            node.left && queue.push(node.left);
            node.right && queue.push(node.right);
        }
        res++;
    }
    return res;
};

595. N叉树的最大深度

递归遍历

/**
 * // Definition for a Node.
 * function Node(val,children) {
 *    this.val = val;
 *    this.children = children;
 * };
 */

/**
 * @param {Node|null} root
 * @return {number}
 */
var maxDepth = function(root) {
    if(!root) return 0;
    let depth = 0;
    for(const node of root.children) {
        depth = Math.max(depth, maxDepth(node))
    }
    return depth + 1;
};

层序遍历

/**
 * // Definition for a Node.
 * function Node(val,children) {
 *    this.val = val;
 *    this.children = children;
 * };
 */

/**
 * @param {Node|null} root
 * @return {number}
 */
var maxDepth = function (root) {
    if (!root) return 0;
    let queue = [], depth = 0;
    queue.push(root);
    while (queue.length) {
        let length = queue.length;
        depth++;
        while (length--) {
            let node = queue.shift();
            for (const child of node.children) {
                queue.push(child);
            }
        }
    }
    return depth;
};

111.二叉树的最小深度

最小深度是从根节点到最近叶子节点的最短路径上的节点数量

在递归单层逻辑中,需要判断左右子树是否为空的情况。

递归遍历

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var minDepth = function (root) {
    if (!root) return 0;
    // 到叶子节点返回1
    if (!root.left && !root.right) return 1;
    // 只有右节点时,递归右节点
    if (!root.left) return 1 + minDepth(root.right);
    // 只有左节点,递归左节点
    if (!root.right) return 1 + minDepth(root.left);
    // 左右节点都有,取一个最小的
    let depth = Math.min(minDepth(root.left), minDepth(root.right)) + 1;
    return depth;
};

层序遍历

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var minDepth = function(root) {
    if(root === null) return 0;
    let queue = [];
    let depth = 0;
    queue.push(root);
    while(queue.length) {
        let length = queue.length;
        depth++;
        while(length--) {
            let node = queue.shift();
            if(!node.left && !node.right) {
                return depth;
            } else {
                node.left && queue.push(node.left);
                node.right && queue.push(node.right);
            }
        } 
    }
};

222. 完全二叉树的节点个数

本题需要利用完全二叉树的性质:除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2^(h-1) 个节点。

完全二叉树的两种情况

  1. 满二叉树。可以直接用 2^树深度 - 1 来计算,注意这里根节点深度为1。
  2. 最后一层叶子节点没有满:分别递归左孩子,和右孩子,递归到某一深度一定会有左孩子或者右孩子为满二叉树,然后依然可以按照情况1来计算。

递归遍历

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var countNodes = function(root) {
    if(root === null) return 0;
    let left = root.left;
    let right = root.right;
    let leftDepth = 0, rightDepth = 0;
    while(left) {
        left = left.left;
        leftDepth++;
    }
    while(right) {
        right = right.right;
        rightDepth++;
    }
    if(leftDepth === rightDepth) {
        return Math.pow(2, leftDepth + 1) - 1;
    }
    return countNodes(root.left) + countNodes(root.right) + 1;
};

层序遍历

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var countNodes = function (root) {
    let queue = [], num = 0;
    if (!root) return num;
    queue.push(root);
    while (queue.length) {
        let length = queue.length;
        while (length--) {
            let node = queue.shift()
            num++;
            node.left && queue.push(node.left);
            node.right && queue.push(node.right);
        }
    }
    return num;
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值