leetcode 归类之 same tree 系列

200 阅读2分钟

100. 相同的树

100. 相同的树

/**
 * 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} p
 * @param {TreeNode} q
 * @return {boolean}
 */
var isSameTree = function(p, q) {
    if (p == null && q == null) {
        return true;
    } else if (p == null || q == null) {
        return false;
    } else if (p.val !== q.val) {
        return false;
    } else {
        return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }
};

101. 对称二叉树

101. 对称二叉树

var isSymmetric = function(root) {
    const check = (leftNode, rightNode) => {
        if (leftNode == null && rightNode == null) return true;
        if (leftNode == null || rightNode == null) return false;
        return leftNode.val == rightNode.val && check(leftNode.left, rightNode.right) && check(leftNode.right, rightNode.left);
    };
    return check(root, root);
};

104. 二叉树的最大深度

104. 二叉树的最大深度

/**
 * 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) {
    if (root == null) {
        return 0;
    }

    const leftNode = maxDepth(root.left);
    const rightNode = maxDepth(root.right);

    const result = Math.max(leftNode, rightNode) + 1;
    return result;
};

110. 平衡二叉树

110. 平衡二叉树

/**
 * 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 {boolean}
 */
/* 获取二叉树的高度 */
const getHeight = (node) => {
    if (node == null) return 0;
    return Math.max(getHeight(node.left), getHeight(node.right)) + 1;
};

var isBalanced = function(root) {
    if (root === null) return true;

    return Math.abs(getHeight(root.left) - getHeight(root.right)) <=1 && isBalanced(root.left) && isBalanced(root.right);
};

111. 二叉树的最小深度

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 == null) return 0;
    let queue = [root];
    let depth = 1;
    while(queue.length) {
        const len = queue.length;
        for(let i = 0; i < len; i++) {
            let node = queue.shift;

            if (node.left == null && node.right == null) {
                return depth;
            }
            if (node.left) queue.push(node.left);
            node.right && queue.push(node.right);
        }
        depth += 1;
    }
    return depth;
};

572. 另一棵树的子树

572. 另一棵树的子树

/**
 * 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
 * @param {TreeNode} subRoot
 * @return {boolean}
 */
var isSubtree = function(root, subRoot) {
    let bool = true
    let res = false
    let subnode = subRoot
    function tra(n1, n2) {
        if (n1 && n2) {
            if (n1.val === n2.val) {
                tra(n1.left, n2.left)
                tra(n1.right, n2.right)
            } else {
                bool = false
            }
        } else {
            if ((!n2 && !n1)) {
                return ;
            } else {
                bool = false
            }
        }
    }
    function traverse(node) {
        // 如果已经找到相同子树了(res === true)就直接返回不再递归了
        // 防止重复的叶子,导致res置false
        if (!node || res) {
            return ;
        }
        if (node.val === subnode.val) {
            // 每次准备dfs子树时,先将标志置true
            // 因为树中可能有重复val的叶子 
            bool = true
            tra(node, subnode)
            res = bool
        }
        traverse(node.left)
        traverse(node.right)
    }
    traverse(root)
    return res
};

965. 单值二叉树

965. 单值二叉树

/**
 * 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 {boolean}
 */
var isUnivalTree = function(root) {
    if (root == null) return true;
    let num = root.val;
    let res = true;
    function dfs (node) {
        if (node == null && !res) return;

        if (node.val !== num) {
            res = false;
            return;
        }
        node.left && dfs(node.left);
        node.right && dfs(node.right);
    }
    dfs(root)
    return res;
};