代码随想录的第十四天(二叉树)

66 阅读2分钟

代码随想录的第十四天(二叉树)

104. 二叉树的最大深度

var maxDepth = function(root) {
    const res = []
    const stack = []
    if (root !== null) stack.push(root)
    while (stack.length) {
        let size = stack.length;
        const arr = []
        while (size--) {
            const cur = stack.shift()
            arr.push(cur.val)
            if (cur.left) stack.push(cur.left)
            if (cur.right) stack.push(cur.right)
        }
        res.push(arr)
    }
    return res.length
};

层序遍历:思路就不多阐述了,前面有对应描述

var maxDepth = function(root) {
    function getHeight(node) {
        if (node === null) return 0
        const left = getHeight(node.left)
        const right = getHeight(node.right)
        let height = 1 + Math.max(left,right)
        return height
    }
    return getHeight(root)
};

后序遍历思路:

1、首先定义终止条件:就是node为null的时候,那会儿是在最底部返回0

2、其次开始遍历左右子树获取最大的一个值进行加一,利用闭包一直循环递增

111. 二叉树的最小深度

var minDepth = function(root) {
    const stack = []
    let res = 0
    if (root !== null) stack.push(root)
    let deep = 0
    while (stack.length) {
        let size = stack.length
        deep++
        while (size--) {
            const cur = stack.shift()
            if (cur.left === null && cur.right === null) {
                return deep
            }
            if (cur.left) stack.push(cur.left)
            if (cur.right) stack.push(cur.right)
        }
    }
    return deep
};

层序遍历思路:

1、主要就是定义一个深度,每次左右节点进行push都进行加一,当左右节点都为空的时候抛出即是最小深度

var minDepth = function(root) {
    function getDeep (root) {
        if (root === null) return 0
        const left = getDeep(root.left)
        const right = getDeep(root.right)
        if (root.left === null) return 1 + right
        if (root.right === null) return 1 + left
        let height = 1 + Math.min(left, right)
        return height
    }
    return getDeep(root)
};

递归法:

1、和求解二叉树的最大深度一样

2、不同在于,因为如果左子树没有节点,那么要去找的就是右子树的最小深度,反之也是如此

3、所以需要先判断左右子树节点是否为空,然后返回相应的深度

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

var countNodes = function(root) {
    const stack = []
    let res = 0
    if (root !== null) stack.push(root)
    while (stack.length) {
        let size = stack.length
        while (size--) {
            res++
            const cur = stack.shift()
            if (cur.left) stack.push(cur.left)
            if (cur.right) stack.push(cur.right)
        }
    }
    return res
};

层序遍历思路:不多说了

var countNodes = function(root) {
    function getNode(root) {
        if (root === null) return 0
        const left = getNode(root.left)
        const right = getNode(root.right)
        let sum = 1+left+right
        return sum
    }
    return getNode(root)
};

递归:后序遍历

var countNodes = function(root) {
    function getNode(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 1+getNode(root.left)+getNode(root.right)
    }
    return getNode(root)
};

利用完全二叉树特性:

1、在终止条件进行判断:当子节点满足完全二叉树的时候可以直接返回当前子树的所有的节点,不再需要往下遍历