面试相关二叉树之LeetCode

92 阅读3分钟

617. 合并二叉树

617. 合并二叉树

/**
 * 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} root1
 * @param {TreeNode} root2
 * @return {TreeNode}
 */
var mergeTrees = function(root1, root2) {
    if (root1 == null && root2 == null) return null;
    if (root1 == null && root2 !== null) return root2;
    if (root1 !== null && root2 == null) return root1;

    const dfs = (root1, root2) => {
        if (root1 == null && root2 == null) return null;
        if (root1 == null && root2 !== null) return root2;
        if (root1 !== null && root2 == null) return root1;

        const root = new TreeNode(root1.val + root2.val);
        root.left = dfs(root1.left, root2.left);
        root.right = dfs(root1.right, root2.right);
        return root;
    }
    return dfs(root1, root2);
};

637. 二叉树的层平均值

637. 二叉树的层平均值

/**
 * 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[]}
 */
/* BFS 广度优先遍历 */
var averageOfLevels = function(root) {
    if (root == null) return [];
    const queue = [root];
    const res = [];

    while (queue.length) {
        let curArr = [];
        const length = queue.length;

        for (let index = 0; index < length; index++) {
            const node = queue.shift();
            curArr.push(node.val);
            if (node.left) {
                queue.push(node.left);
            }
            if (node.right) {
                queue.push(node.right);
            }
            
        }
        /* 取总数 */
        let sum = curArr.reduce((prev, current) => current + prev);
        /* 计算平均值 */
        res.push(sum / curArr.length);
    }
    return res;
};

剑指 Offer 27. 二叉树的镜像

简单

剑指 Offer 27. 二叉树的镜像

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {TreeNode}
 */
var mirrorTree = function(root) {
    if (root == null) return null;
    const dfs = (root) => {
        if (root == null) return null;
        const node = new TreeNode(root.val);
        node.left = dfs(root.right);
        node.right = dfs(root.left);
        return node;
    }
    return dfs(root);
};

剑指 Offer 28. 对称的二叉树

简单

剑指 Offer 28. 对称的二叉树

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isSymmetric = function(root) {
    if(root == null) return true;
    const help = (left, right) => {
        if(left == null && right == null) return true;
        if (left == null || right == null || left.val !== right.val) {
            return false;
        }
        return help(left.left, right.right) && help(left.right, right.left);
    }
    return help(root.left, root.right);
};

剑指 Offer 32 - II. 从上到下打印二叉树 II

简单

剑指 Offer 32 - II. 从上到下打印二叉树 II

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[][]}
 */
/* 
    BFS
*/
var levelOrder = function(root) {
    if (root == null) return [];
    const queue = [root];
    const res = [];
    while (queue.length) {
        const length = queue.length;
        let arr = [];

        for (let index = 0; index < length; index++) {
            const node = queue.shift();
            arr.push(node.val);
            if (node.left) {
                queue.push(node.left);
            }
            if (node.right) {
                queue.push(node.right);
            }
            
        }
        res.push(arr);
    }
    return res;
};

剑指 Offer 54. 二叉搜索树的第k大节点

简单

剑指 Offer 54. 二叉搜索树的第k大节点

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} k
 * @return {number}
 */
var kthLargest = function(root, k) {
    if (root == null) return 0;
    let arr = [];
    const dfs = (root) => {
        if (root == null) return;
        dfs(root.left);
        arr.push(root.val);
        dfs(root.right);
    }
    dfs(root);
    arr.reverse();
    return arr[k - 1];
};

剑指 Offer 55 - II. 平衡二叉树

简单

剑指 Offer 55 - II. 平衡二叉树

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
// 左右子树的深度不超过1,就是平衡二叉树
var isBalanced = function(root) {
    if (root==null) return true;

    const getHeight = (node) => {
        if (node== null) return 0;
        return Math.max(getHeight(node.left), getHeight(node.right)) + 1;
    }
    return Math.abs(getHeight(root.left) - getHeight(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
};

剑指 Offer 68 - I. 二叉搜索树的最近公共祖先

简单

剑指 Offer 68 - I. 二叉搜索树的最近公共祖先

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @param {TreeNode} p
 * @param {TreeNode} q
 * @return {TreeNode}
 */
/* 
    经典题目
*/
var lowestCommonAncestor = function(root, p, q) {
    if (root == null) return null;

    const dfs = (root) => {
        if (root == null) return null;

        if (root.val == p.val || root.val == q.val) {
            return root;
        }
        let left = dfs(root.left);
        let right = dfs(root.right);
        if (left && right) {
            return root;
        }
        if (left == null) {
            return right;
        }
        return left;
    }
    return dfs(root);
};
/* 这个方式真不错 */
var lowestCommonAncestor = function(root, p, q){
  if(p.val < root.val && q.val < root.val){
    return lowestCommonAncestor(root.left, p, q);
  }else if(p.val > root.val && q.val > root.val){
    return lowestCommonAncestor(root.right, p, q);
  }else return root;
}

剑指 Offer 68 - II. 二叉树的最近公共祖先

简单

剑指 Offer 68 - II. 二叉树的最近公共祖先

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @param {TreeNode} p
 * @param {TreeNode} q
 * @return {TreeNode}
 */
var lowestCommonAncestor = function(root, p, q) {
    if (root == null) return null;
    const dfs = (root) => {
        if (root == null) return null;
        if (root.val == p.val || root.val == q.val) {
            return root;
        }
        let left = dfs(root.left);
        let right = dfs(root.right);

        if (left && right) {
            return root;
        }
        if (left == null) {
            return right;
        }
        return left;
    }
    return dfs(root);
};

剑指 Offer II 052. 展平二叉搜索树

简单

剑指 Offer II 052. 展平二叉搜索树

/**
 * 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 {TreeNode}
 */
/* 
    先转成数组
    在一个个变成新的搜索树
*/
var increasingBST = function(root) {
    if (root == null) return null;
    let prev = new TreeNode(-1);
    let res = [];
    const dfs = (root) => {
        if (root == null) return;
        dfs(root.left, prev.right);
        res.push(root.val);
        dfs(root.right, prev.right);
    }
    dfs(root);
    /* 指针 */
    let cur = prev;
    for (const iterator of res) {
        cur.right = new TreeNode(iterator);
        cur = cur.right;
    }

    return prev.right;
};

剑指 Offer II 056. 二叉搜索树中两个节点之和

剑指 Offer II 056. 二叉搜索树中两个节点之和

/**
 * 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 {number} k
 * @return {boolean}
 */
/* 
    中序遍历 + 双指针
*/
var findTarget = function(root, k) {
    let arr = [];
    const inorder = (root) => {
        if (root == null) return;
        inorder(root.left);
        arr.push(root.val);
        inorder(root.right);
    }
    inorder(root);
    let left = 0;
    let right = arr.length - 1;
    while (left < right) {
        if (arr[left] + arr[right] > k) {
            right--;
        } else if (arr[left] + arr[right] < k) {
            left++;
        } else {
            return true;
        }
    }
    return false;
};

面试题 04.02. 最小高度树

面试题 04.02. 最小高度树

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {number[]} nums
 * @return {TreeNode}
 */
var sortedArrayToBST = function(nums) {
    if (!nums.length) return null;
    let i = Math.floor(nums.length / 2);
    const root = new TreeNode(nums[i]);
    root.left = sortedArrayToBST(nums.slice(0, i));
    root.right = sortedArrayToBST(nums.slice(i + 1));
    return root;
};

面试题 04.04. 检查平衡性

简单

面试题 04.04. 检查平衡性

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isBalanced = function(root) {
    if (root == null) return true;
    const help = (root) => {
        if(root == null) return 0;
        let left = help(root.left);
        let right = help(root.right);
        return Math.max(left, right) + 1;
    }
    return Math.abs(help(root.left) - help(root.right)) <=1 &&  isBalanced(root.left) && isBalanced(root.right)
};

面试题 17.12. BiNode

面试题 17.12. BiNode

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {TreeNode}
 */
/* 
    这个题目有两个解决方案
    1, 用中序遍历,获取值的数组,然后再创建一个新的树
    2, 在原来的树上进行操作,使用指针的形式
*/
var convertBiNode = function(root) {
    if (root == null) return null;
    let prev= new TreeNode(-1);
    let res = prev;
    const inorder = (root) => {
        if (root == null) return null;
        inorder(root.left);

        root.left = null;
        prev.right = root;
        /* 指针指向下一位 */
        prev = root;

        inorder(root.right);
    }
    inorder(root);
    return res.right;
};