JavaScript刷LeetCode拿offer-树的遍历

news2024/9/20 10:53:03

什么是树

  • 一种分层数据的抽象模型。
  • 前端工作中常见的树包括:DOM树,级联选择,树形控件
  • JS中没有树,可以用Object和Array构建树
  • 树的常用操作:深度/广度优先遍历先中后序遍历

深度优先遍历

  • 访问根节点
  • 对根节点的children挨个进行深度优先遍历

代码展示:

const tree = {
    val: 'a',
    children: [
        {
            val: 'b',
            children: [
                {
                    val: 'd',
                    children: []
                },
                {
                    val: 'e',
                    children: []
                }
            ]
        },
        {
            val: 'c',
            children: [
                {
                    val: 'f',
                    children: []
                },
                {
                    val: 'g',
                    children: []
                }
            ]
        }
    ],
};

const dfs = (root) => {
  console.log(root.val);
  root.children.forEach(dfs);
}

dfs(tree);

输出顺序:a -> b -> d -> e -> c -> f -> g

广度优先遍历

  • 新建一个队列,把根节点入队
  • 队头出队并访问
  • 队头的children分别入队
  • 重复二,三步,直到队列为空

代码展示:

const bfs = (root) => {
    const q = [root];
    while (q.length) {
        const n = q.shift();
        console.log(n.val);
        n.children.forEach((child) => {
            q.push(child);
        })
    }
}

bfs(tree);  

输出顺序:a -> b -> c -> d -> e -> f -> g

先序遍历

  • 访问根节点
  • 对根节点的左子树进行先序遍历
  • 对根节点的右子树进行先序遍历

代码展示:

const bt = {
    val: 1,
    left: {
        val: 2,
        left: {
            val: 4,
            left: null,
            right: null
        },
        right: {
            val: 5,
            left: null,
            right: null
        }
    },
    right: {
        val: 3,
        left: {
            val: 6,
            left: null,
            right: null
        },
        right: {
            val: 7,
            left: null,
            right: null
        }
    }
};
// 递归
const preorder = (root) => {
    if (!root) return;
    console.log(root.val);
    preorder(root.left);
    preorder(root.right);
}

preorder(tree);

// 迭代
const preorder = (root) => {
    if (root) return;
    const stack = [root];
    while (stack.length) {
        const n = stack.pop();
        console.log(top.val);
        if (n.right) stack.push(n.right);
        if (n.left) stack.push(n.left);
    }
  }
}

preorder(tree);

参考视频:传送门

输出顺序:1 -> 2 -> 4 -> 5 -> 3 -> 6 -> 7

中序遍历

  • 对根节点的左子树进行中序遍历
  • 访问根节点
  • 对根节点的右子树进行中序遍历

代码展示:

// 递归
const inorder = (root) => {
    if (!root) return;
    preorder(root.left);
    console.log(root.val);
    preorder(root.right);
}

inorder(tree);

// 迭代
const inorder = (root) => {
    if (!root) return;
    const stack = [];
    let p = root;
    while (stack.length || p) {
        while (p) {
            stack.push(p);
            p = p.left;
        }
        const n = stack.pop();
        console.log(n.val);
        p = n.right;
    }
}

inorder(tree);

输出顺序:4 -> 2 -> 5 -> 1 -> 6 -> 3 -> 7

后序遍历

  • 对根节点的左子树进行后序遍历
  • 对根节点的右子树进行后序遍历
  • 访问根节点

代码展示:

// 递归
const postorder = (root) => {
    if (!root) return;
    preorder(root.left);
    preorder(root.right);
    console.log(root.val);
}

postorder(tree);

// 迭代
const postorder = (root) => {
    if (!root) return;
    const stack = [root];
    const outputStack = [];
    while (stack.length) {
        const n = stack.pop();
        outputStack.push(n);
        if (n.left) stack.push(n.left);
        if (n.right) stack.push(n.right);
    }
    while (outputStack) {
        const n = outputStack.pop();
        console.log(n.val);
    }
}

postorder(tree);

输出顺序:4 -> 5 -> 2 -> 6 -> 7 -> 3 -> 1

leetcode题目

难度:简单

  1. 二叉树的最大深度

思路:

  • 求最大深度,优先考虑深度优先遍历
  • 在深度优先遍历过程中,记录每个节点所在的层级,找到最大的层级即可

代码展示:

/**
 * @param {TreeNode} root
 * @return {number}
 */
var maxDepth = function(root) {
  let maxDepth = 0;
  const dfs = (n, l) => {
    if (!n) return;
    dfs(n.left, l + 1);
    dfs(n.right, l + 1);
    if (!n.left && !n.right) maxDepth = Math.max(maxDepth, l);
  }
  dfs(root, 1);
  return maxDepth;
};

复杂度分析:

  • 时间复杂度:O(n):n为二叉树节点个数。

  • 空间复杂度:O(height):height为二叉树的最大深度。在平均情况下,二叉树的高度与节点个数为对数关系,即 O(logN)。而在最坏情况下,树形成链状,空间复杂度为 O(N)。

  1. 翻转二叉树

思路:

  • 方法一使用广度优先遍历,在遍历树的过程中,交换当前层级下的左右子树
  • 方法二使用递归解决,递归最重要的是定义子问题。

代码展示:

方法一:广度优先遍历

/**
 * @param {TreeNode} root
 * @return {TreeNode}
 */
var invertTree = function(root) {
    if (!root) root;
    const bfs = (root) => {
        const q  = [root];
        while (q.length) {
            const n = q.shift();
            const temp = n.right;
            n.right = n.left;
            n.left = temp;
            if (n.left) q.push(n.left);
            if (n.right) q.push(n.right);
        }
    }
    bfs(root);
    return root;
}

方法二:递归

/**
 * @param {TreeNode} root
 * @return {TreeNode}
 */
var invertTree = function(root) {
  if (!root) return null;
  let newTree = new TreeNode(root.val);
  newTree.left = invertTree(root.right);
  newTree.right = invertTree(root.left);
  return newTree;
};

复杂度分析:

  • 时间复杂度:O(n):n为二叉树节点个数。

  • 空间复杂度:O(height):height为二叉树的最大深度。在平均情况下,二叉树的高度与节点个数为对数关系,即 O(logN)。而在最坏情况下,树形成链状,空间复杂度为 O(N)。

  1. 对称二叉树

思路:

  • 通过遍历比较两个相同根节点的左子树和右子树的值是否相等
  • 如果每次都相等,直到两个节点都不存在,说明是对称的
  • 如果两个节点不相等,则说明不对称

代码展示:

/** * @param {TreeNode} root * @return {boolean} */
var isSymmetric = function(root) {
    if (!root) return false;
    const checkSym = (p, q) => {
        if (!p && !q) return true;
        if (!p || !q) return false;
        return p.val === q.val && checkSym(p.left, q.right) && checkSym(q.left, p.right);
    }
    return checkSym(root, root);
}

复杂度分析:

  • 时间复杂度:O(n):n为二叉树节点数。

  • 空间复杂度:O(n):n为二叉树节点数,最差情况为O(n)。

  1. 二叉树的直径

在这里插入图片描述

思路:

  • 考虑深度优先遍历
  • 寻找两个最深的节点距离之和

代码展示:

/**
 * @param {TreeNode} root
 * @return {number}
 */
var diameterOfBinaryTree = function(root) {
  if (!root) return 0;
  let res = 1; // 默认为根节点的路径长度
  const dfs = (root) => {
    if (!root) return 0;
    let L = dfs(root.left);  // 左子树深度,上图为例,最长L为2
    let R = dfs(root.right);  // 右子树深度,上图为例,最长R为1
    res = Math.max(res, L + R + 1); // 最大L+R+1,+1为根节点,总共深度为4,即节点树为4
    return Math.max(L, R) + 1;  // 包含根节点的深度,上图为例,最长L为2,最长R为1
  }
  dfs(root);
  return res - 1; // 最终结果要得到直径为3
};

复杂度分析:

  • 时间复杂度:O(n):n为二叉树节点数。
  • 空间复杂度:O(height):height为二叉树的最大深度。在平均情况下,二叉树的高度与节点个数为对数关系,即 O(logN)。而在最坏情况下,树形成链状,空间复杂度为 O(N)。

剑指 Offer 27. 二叉树的镜像

思路:

  • 逐层递归互换左右子树节点的位置

代码展示:

/**
 * @param {TreeNode} root
 * @return {TreeNode}
 */
var mirrorTree = function(root) {
    if (!root) return root;
    const temp = root.left;
    root.left = root.right;
    root.right = temp;
    mirrorTree(root.left);
    mirrorTree(root.right);
    return root;
}

优化后:

/** * @param {TreeNode} root * @return {TreeNode} */
var mirrorTree = function(root) {
    if (!root) return root;
    [root.left, root.right] = [mirrorTree(root.right), mirrorTree(root.left)];
    return root;
}

复杂度分析:

  • 时间复杂度:O(n):n为二叉树节点数。

  • 空间复杂度:O(n):n为二叉树节点树。

  1. 二叉树的最近公共祖先

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

思路:

  • 递归

代码展示:

/** * @param {TreeNode} root * @param {TreeNode} p * @param {TreeNode} q * @return {TreeNode} */
var lowestCommonAncestor = function(root, p, q) {
  // 当传入递归的树等于 p 或者等于 q,说明找到了 p 或者 q,返回给递归调用的 l 或 r
  if (!root || p === root || q === root) return root;
  let l = lowestCommonAncestor(root.left, p, q); // 递归调用,寻找 p 和 q
  let r = lowestCommonAncestor(root.right, p, q); // 递归调用,寻找 p 和 q
  return l && r ? root : l || r; 
  // 如果 p 和 q 分别在 root.left 和 root.right 中找到,则根节点 root 成为最近的公共祖先返回。
  // 假如 p 和 q 在 root.left 中找到,递归会把 p 和 q 的公共祖先返回给 l。
  // 假如,p 和 q 在 root.right 中找到,递归会把 p 和 q 的公共祖先返回给 r。
  // 根节点root,l 或 r 最终成为当前函数的返回值。
};

复杂度分析:

  • 时间复杂度:O(n):n为二叉树节点数。
  • 空间复杂度:O(n):n为二叉树节点数。递归调用的栈深度取决于二叉树的高度,二叉树最坏情况下为一条链,此时高度为 N,因此空间复杂度为 O(N)。

剑指 Offer 55 - I. 二叉树的深度

思路:

  • 考虑深度优先遍历

代码展示:

var maxDepth = function (root) {
    if (!root) return 0;
    let max = 0;
    const dfs = (root, l) => {
        if (root.left) dfs(root.left, l + 1);
        if (root.right) dfs(root.right, l + 1);
        if (!root.left && !root.right) max = Math.max(max, l);
    }
    dfs(root, 1);
    return max;
}

复杂度分析:

  • 时间复杂度:O(n):n为二叉树节点数,计算树的深度需要遍历所有节点。

  • 空间复杂度:O(n):最差情况下,空间复杂度为O(n)。

  1. 二叉树的所有路径

思路:

  • 本题考虑使用深度优先遍历。
  • 如果当前节点有左子树右子树,就递归调用函数,直到左右子树都不存在,此时就是我们要找的的路径。

代码展示:

/**
 * @param {TreeNode} root
 * @return {string[]}
 */
var binaryTreePaths = function(root) {
  if (!root) return [];
  const res = [];
  const dfs = (root, path) => {
      if (root) {
          path += root.val;
          if (!root.left && !root.right) {
              res.push(path);
          } else {
              path += '->';
              dfs(root.left, path);
              dfs(root.right, path);
          }
      }
  }
  dfs(root, "");
  return res;
};

复杂度分析:

  • 时间复杂度:O(n^2):n为二叉树节点数。在深度优先搜索中每个节点会被访问一次且只会被访问一次,每一次会对 path 变量进行拷贝构造,时间代价为 O(N),故时间复杂度为 O(N^2)。
  • 空间复杂度:O(n^2):n为二叉树节点数。

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

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

  • 解题方法同二叉树的层序遍历
  1. 平衡二叉树

思路:

  • 考虑深度优先遍历

  • 算出最大深度和最小深度的差值,即可判断是否为平衡二叉树 (本题和求二叉树直径做法类似)

代码展示:

/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isBalanced = function(root) {
  if (!root) return true;
  let diff = 0;
  const dfs = (root) => {
    if (!root) return 0;
    let L = dfs(root.left);
    let R = dfs(root.right);
    diff = Math.max(diff, Math.abs(R - L));
    return Math.max(L, R) + 1;
  }
  dfs(root);
  return diff - 1 <= 0;
};

复杂度分析:

  • 时间复杂度:O(n):n为二叉树节点数。

  • 空间复杂度:O(n):n为二叉树节点数,最差情况为O(n)。

  1. 合并二叉树

思路:

  • 递归对两个相同位置的节点相加

代码展示:

/**
 * @param {TreeNode} root1
 * @param {TreeNode} root2
 * @return {TreeNode}
 */
var mergeTrees = function(root1, root2) {
  if (!root1 || !root2) return root1 || root2;
  let newRoot = new TreeNode(root1.val + root2.val);
  newRoot.left = mergeTrees(root1.left, root2.left);
  newRoot.right = mergeTrees(root1.right, root2.right);
  return newRoot;
};

复杂度分析:

  • 时间复杂度:O(n):n为二叉树节点数。

  • 空间复杂度:O(n):n为二叉树节点数。

  1. 路径总和

思路:

  • 考虑深度优先遍历
  • 记录从根节点到当前节点的和,与target比较。

代码展示:

/** * @param {TreeNode} root * @param {number} targetSum * @return {boolean} */
var hasPathSum = function(root, targetSum) {
  if (!root) return false;
  let res = false;
  const dfs = (root, val) => {
    if (root.left) dfs(root.left, val + root.left.val);
    if (root.right) dfs(root.right, val + root.right.val);
    if (!root.left && !root.right && val === targetSum) res = true;
  }
  dfs(root, root.val);
  return res;
};

复杂度分析:

  • 时间复杂度:O(n):n为二叉树节点数。

  • 空间复杂度:O(n):n为二叉树节点数。

  1. 二叉树的最小深度

思路:

  • 方法一考虑使用广度优先遍历

  • 方法二考虑使用深度优先遍历

代码展示:

方法一:

/**
 * @param {TreeNode} root
 * @return {number}
 */
var minDepth = function(root) {
    if (!root) return 0;
    let minDep = Infinity;
    const bfs = (root, l) => {
        const q = [[root, l]];
        while (q.length) {
            const [n, l] = q.shift();
            if (n.left) q.push([n.left, l + 1]);
            if (n.right) q.push([n.right, l + 1]);
            if (!n.left && !n.right) minDep = Math.min(minDep, l);
        }
    }
    bfs(root, 1);
    return minDep;
};

方法二:

/** * @param {TreeNode} root * @return {number} */
var minDepth = function(root) {
  if (!root) return 0;
  if (root.left && root.right) {
    return 1 + Math.min(minDepth(root.left), minDepth(root.right));
  } else if (root.left) {
    return 1 + minDepth(root.left);
  } else if (root.right) {
    return 1 + minDepth(root.right);
  } else {
    return 1;
  }
};

复杂度分析:

  • 时间复杂度:O(n):n为二叉树节点数。

  • 空间复杂度:O(n):n为二叉树节点数。

  1. N 叉树的前序遍历

思路:

  • 类似于二叉树的前序遍历

代码展示:

// 递归
var preorder = function(root) {
  if (!root) return [];
  const res = [];
  const preord = (n) => {
    if (n) res.push(n.val);
    n.children.forEach(preord);
  }
  preord(root);
  return res;
};

// 迭代
var preorder = function(root) {
  if (!root) return [];
  const stack = [root];
  const res = [];
  while (stack.length) {
    const n = stack.pop();
    res.push(n.val);
    n.children.reverse().forEach(child => {
      stack.push(child);
    });
  }
  return res;
}

复杂度分析:

  • 时间复杂度:O(n):n为二叉树节点数。
  • 空间复杂度:O(height):height为二叉树最大深度。

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

思路:

  • 根据树的特点,采用中序遍历,按右子树 - 根节点 - 左子树的顺序遍历,就可以由大到小找到第k大节点

代码展示:

/**
 * @param {TreeNode} root
 * @param {number} k
 * @return {number}
 */
var kthLargest = function(root, k) {
  if (!root || k <= 0) return null;
  const stack = [];
  const res = null;
  let p = root;
  while (stack.length || p) {
    while (p) {
      stack.push(p);
      p = p.right;
    }
    const top = stack.pop();
    if (--k === 0) return top.val;
    p = top.left;
  }
  return res;
};

复杂度分析:

  • 时间复杂度:O(n):n为二叉树节点数。
  • 空间复杂度:O(n):n为二叉树节点数。

难度:中等

  1. 二叉树的前序遍历

代码展示:

/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var preorderTraversal = function(root) {
  if (!root) return [];
  const stack = [root];
  const res = [];
  while (stack.length) {
    const n = stack.pop();
    res.push(n.val);
    if (n.right) stack.push(n.right);
    if (n.left) stack.push(n.left);
  }
  return res;
};

复杂度分析:

  • 时间复杂度:O(n):n为二叉树节点数。

  • 空间复杂度:O(n)

  1. 二叉树的中序遍历

代码展示:

/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var inorderTraversal = function(root) {
  if (!root) return [];
  const stack = [];
  const res = [];
  let p = root;
  while (stack.length || p) {
    while (p) {
      stack.push(p);
      p = p.left;
    }
    const n = stack.pop();
    res.push(n.val);
    p = n.right;
  }
  return res;
};

复杂度分析:

  • 时间复杂度:O(n):n为二叉树节点数。

  • 空间复杂度:O(n)

  1. 二叉树的后序遍历

代码展示:

/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var postorderTraversal = function(root) {
  if (!root) return [];
  const stack = [root];
  const outputStack = [];
  const res = [];
  while (stack.length) {
      const n = stack.pop();
      outputStack.push(n);
      if (n.left) stack.push(n.left);
      if (n.right) stack.push(n.right);
  }
  while (outputStack.length) {
      const n = outputStack.pop();
      res.push(n.val);
  }
  return res;
};

复杂度分析:

  • 时间复杂度:O(n):n为二叉树节点数。

  • 空间复杂度:O(n)

  1. 二叉树的层序遍历

代码展示:

方法一:深度优先遍历

/**
 * @param {TreeNode} root
 * @return {number[][]}
 */
var levelOrder = function(root) {
  if (!root) return [];
  const res = [];
  const dfs = ([root, l]) => {
    if (!res[l]) {
      res[l] = [root.val];
    } else {
      res[l].push(root.val)
    }
    if (root.left) dfs([root.left, l + 1]);
    if (root.right) dfs([root.right, l + 1]);
  }
  dfs([root, 0]);
  return res;
};

方法二:广度优先遍历

/**
 * @param {TreeNode} root
 * @return {number[][]}
 */
var levelOrder = function(root) {
  if (!root) return [];
  let res = [];
  const bfs = (root, l) => {
    const q = [[root, l]];
    while (q.length) {
      const [n, l] = q.shift();
      if (!res[l]) res[l] = [];
      res[l].push(n.val);
      if (n.left) q.push([n.left, l + 1]);
      if (n.right) q.push([n.right, l + 1]);
    }
  };
  bfs(root, 0);
  return res;
};

复杂度分析:

  • 时间复杂度:O(n):n为二叉树节点数。

  • 空间复杂度:O(n)

  1. 从前序与中序遍历序列构造二叉树

  2. 从中序与后序遍历序列构造二叉树

剑指 Offer 07. 重建二叉树

思路:

  • 递归

代码展示:

参考:多种解法,逐渐优化

/**
 * @param {number[]} preorder
 * @param {number[]} inorder
 * @return {TreeNode}
 */
var buildTree = function(preorder, inorder) {
  if (!preorder.length || !inorder.length) return null;
  const map = new Map();
  for (let i = 0; i < inorder.length; i++) {
    map.set(inorder[i], i);
  }
  const builder = (p_start, p_end, i_start, i_end) => {
    if (p_start > p_end) return null;
    let rootVal = preorder[p_start]; // 找到根节点
    let root = new TreeNode(rootVal); // 设置二叉树的根节点
    let mid = map.get(rootVal);  // 找到根节点在inorder中的位置
    let leftNum = mid - i_start; // 左子树的个数
    root.left = builder(p_start + 1, p_start + leftNum, i_start, mid - 1);
    root.right = builder(p_start + leftNum + 1, p_end, mid + 1, i_end);
    return root;
  }
  return builder(0, preorder.length - 1, 0, inorder.length - 1);
};

复杂度分析:

  • 时间复杂度:O(n):n为二叉树节点数。

  • 空间复杂度:O(n)

  1. 二叉树的锯齿形层序遍历

思路:

  • 同二叉树层序遍历

代码展示:

/**
 * @param {TreeNode} root
 * @return {number[][]}
 */
var zigzagLevelOrder = function(root) {
  if (!root) return [];
  const res = [];
  const bfs = ([root, index]) => {
    if (!root) return;
    if (!res[index]) {
      res[index] = [root.val];
    } else {
      index % 2 === 0 ? res[index].push(root.val) : res[index].unshift(root.val);
    }
    if (root.left) bfs([root.left, index + 1]);
    if (root.right) bfs([root.right, index + 1]);
  }
  bfs([root, 0]);
  return res;
};

复杂度分析:

  • 时间复杂度:O(n):n为二叉树节点数。

  • 空间复杂度:O(n)

  1. 二叉树的右视图

思路:

  • 方法一考虑广度优先遍历,每层保留最后一个元素

  • 方法二考虑使用类似先序遍历根 - 右 - 左的方式遍历,每层第一个出现的元素保留下来即可

代码展示:

方法一:广度优先遍历

/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var rightSideView = function(root) {
  if (!root) return [];
  const res = [];
  const bfs = (root, l) => {
    const q = [[root, l]]
    while (q.length) {
      const [n, l] = q.shift();
      res[l] = n.val;
      if (n.left) q.push([n.left, l + 1]);
      if (n.right) q.push([n.right, l + 1]);
    }
  }
  bfs(root, 0);
  return res;
};

方法二:

/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var rightSideView = function(root) {
  if (!root) return [];
  const res = [];
  const stack = [[root, 0]];
  while (stack.length) {
    const [n, l] = stack.pop();
    if (res[l] == undefined) res.push(n.val);
    if (n.left) stack.push([n.left, l + 1]);
    if (n.right) stack.push([n.right, l + 1]);
  }
  return res;
};

复杂度分析:

  • 时间复杂度:O(n):n为二叉树节点数。
  • 空间复杂度:O(n)

剑指 Offer 26. 树的子结构

思路:

  • 递归比较

代码展示:

/** * @param {TreeNode} A * @param {TreeNode} B * @return {boolean} */
var isSubStructure = function(A, B) {
  if (!A || !B) return false;
  const isSameSub = (p, q) => {
    if (!q) return true;
    if (!p) return false;
    if (p.val !== q.val) return false;
    return isSameSub(p.left, q.left) && isSameSub(p.right, q.right);
  }
  return isSameSub(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
};

复杂度分析:

  • 时间复杂度:O(mn):m,n分别为A和B的节点数。

  • 空间复杂度:O(m)

  1. 验证二叉搜索树

代码展示:

/** * @param {TreeNode} root * @return {boolean} */
var isValidBST = function(root) {
  const helper = (root, lower, upper) => {
    if (root === null) {
      return true;
    }
    if (root.val <= lower || root.val >= upper) {
      return false;
    }
    return helper(root.left, lower, root.val) && helper(root.right, root.val, upper);
  }
  return helper(root, -Infinity, Infinity);
};

复杂度分析:

  • 时间复杂度:O(n):n为二叉树节点数。

  • 空间复杂度:O(n)

  1. 不同的二叉搜索树

思路:

  • 卡塔兰数公式

代码展示:

var numTrees = function(n) {
    let C = 1;
    for (let i = 0; i < n; ++i) {
        C = C * 2 * (2 * i + 1) / (i + 2);
    }
    return C;
};

复杂度分析:

  • 时间复杂度:O(n):n为二叉树节点数。

  • 空间复杂度:O(1)

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

代码展示:

/** * @param {TreeNode} root * @return {number} */
var countNodes = function(root) {
  return root ? countNodes(root.left) + countNodes(root.right) + 1 : 0;
};

复杂度分析:

  • 时间复杂度:O(n):n为二叉树节点数。
  • 空间复杂度:O(1)

剑指 Offer 34. 二叉树中和为某一值的路径

代码展示:

/**
 * @param {TreeNode} root
 * @param {number} target
 * @return {number[][]}
 */
var pathSum = function(root, target) {
  if (!root) return [];
  const res = [];
  let temp = [];
  const dfs = (root, v) => {
    if (!root) return null;
    temp.push(root.val);
    if (root.left) dfs(root.left, root.left.val + v);
    if (root.right) dfs(root.right, root.right.val + v);
    if (!root.left && !root.right && v === target) {
      res.push([...temp]);
    }
    temp.pop();
  }
  dfs(root, root.val);
  return res;
};

复杂度分析:

  • 时间复杂度:O(n):n为二叉树节点数。
  • 空间复杂度:O(n)

难度:困难

  1. 二叉树中的最大路径和

代码展示:

参考解法

/**
 * @param {TreeNode} root
 * @return {number}
 */
var maxPathSum = function(root) {
  let maxNum = Number.MIN_SAFE_INTEGER;
  const dfs = (root) => {
    if (!root) return 0;
    const left = dfs(root.left);
    const right = dfs(root.right);
    const curMaxSum = left + root.val + right; // 当前子树内部最大路径和
    maxNum = Math.max(maxNum, curMaxSum);
    const outputMaxSum = root.val + Math.max(left, right); // 当前子树对上一层输出的最大路径和
    return outputMaxSum > 0 ? outputMaxSum : 0; // 大于0有输出的必要,小于0就返回0
  };
  dfs(root);
  return maxNum;
}

复杂度分析:

  • 时间复杂度:O(n):n为二叉树节点数。
  • 空间复杂度:O(n)

剑指 Offer 37. 序列化二叉树

总结

继续对树的深度/广度优先遍历,先中后序遍历,层序遍历等遍历和递归的方法,有更深入的理解和学习。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/142158.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Windows Server【开机启动和任务计划程序】实现服务器重启后项目自启动(Windows Server 任务计划程序无法执行问题处理)

1.问题说明 有些时候我们希望计算机开机后就启动一些服务或应用程序&#xff0c;比如远程工具。这里介绍两种方式。 2.开机启动 使用WinR调出运行&#xff0c;输入&#xff1a; 1️⃣ shell:startup 用户开机自启动&#xff08;程序开机自启动只针对当前登录的用户&#xf…

2分布式微服务技术栈-SpringCloud<Feign>

分布式微服务技术栈Feign HTTP 客户端Feign-基于Feign远程调用Feign-自定义配置Feign HTTP 客户端 Feign-基于Feign远程调用 声明式 事务 spring 声明一个 远程调用 封装 所有 对 userservice 的 远程调用 类似于 controller 的 注解 把接口 注入进来 不仅是 http 客户端 …

Node.js 中的模块化

1、模块化的基本概念 1.1、什么是模块化 模块化是指解决一个复杂问题时&#xff0c;自顶向下逐层把系统划分成若干模块的过程。对于整个系统来说&#xff0c;模块是可组合、分解和更换的单元。 1.2、编程领域中的模块化 编程领域中的模块化&#xff0c;就是遵守固定的规则&a…

探索性数据分析(Exploratory Data Analysis,EDA)

目录参考资料PART 02 探索性数据分析 探索性数据分析(Exploratory Data Analysis&#xff0c;简称EDA)&#xff0c;指对数据分析的过程中尽量不加入先验假设&#xff0c;而是通过作图表和统计等方式来探索数据结构和规律。 EDA最早由John W. Tukey在上世纪70年代被提出&#…

批量下载线上数据文件--业务诉求

诉求接到现场运维诉求&#xff0c;需要获取到指定业务节点的所有附件文件分析某个节点的业务信息&#xff0c;可以通过sql来筛选出全部需要的关联字段信息以及文件磁盘路径和上传路径通过脚本或命令&#xff0c;将sql筛选到的路径文件统一复制到指定目录将复制到的文件下载&…

再学C语言25:分支和跳转——continue、break和switch

一般地&#xff0c;进入循环体后&#xff0c;在下次循环判断之前程序执行循环体中所有语句 一、continue语句 continue&#xff1a;循环中&#xff0c;当运行到该语句时&#xff0c;其将导致剩余的迭代部分被忽略&#xff0c;开始下一次迭代 如果continue处于嵌套结构中&…

【深度学习】李宏毅2021/2022春深度学习课程笔记 - 各式各样神奇的自注意力机制(Self-Attention)变型

文章目录一、Self-Attention 各式各样的变型二、How to make self-attention efficient&#xff1f;三、Notice四、Local Attention / Truncated Attention五、Stride Attention六、Global Attention七、Many Different Choices八、Can we only focus on Critical Parts?8.1 C…

车牌识别应用搭建(含模型和源码)

车牌识别应用搭建 内容说明 本示例旨在展示如何在 DeepStream SDK 版本不低于 5.0.1 的情况下使用分级模型进行检测和分类。 本例中的模型均为TAO3.0模型。 PGIE(car detection) -> SGIE(car license plate detection) -> SGIE(car license plate recognization) 该流…

Linux系统下的rpm/yum管理

文章目录Linux系统下的rpm管理1.介绍2.rpm包的简单查询指令3.rpm包的其它查询指今4.卸载rpm包5.rpm6.yumLinux系统下的rpm管理 1.介绍 rpm用于互联网下载包的打包及安装工具&#xff0c;它包含在某些Linux分发版中。它生成具有.RPM扩展名的文件。RPM是RedHat Package Manager…

Java 集合的介绍和使用

1.什么是集合&#xff1f; 对一些数据的存储就叫做集合&#xff0c;相比于数组&#xff0c;这是一种动态的集合。 1.可以动态的保存任意多个对象 2.提供一些动态操作集合的方法比如&#xff1a;add ,remove ,set ,get 等。 3.使用集合更加方便&#xff0c;提高代码编写效率。…

创建 ASP.NET Core MVC 项目

目录 一、创建ASP.NET Core MVC项目 二、ASP.NET Core MVC目录结构 一、创建ASP.NET Core MVC项目 打开Visual Studio 2022 点击创建新项目 在列表中找到:ASP.NET Core Web应用(模型-试图-控制器):用于创建包含示例ASP.Net Core Mvc视图和控制器的Asp.NET Core应用程序…

Docker如何安装nacos最新版本2.2.0

本文介绍如何在docker容器下安装部署nacos最新版本2.2.0。一、单机本地配置1.1 拉取nacos镜像执行以下命令从docker远端拉取nacos镜像文件。docker pull nacos/nacos-server1.2 启动nacos镜像执行以下命令&#xff0c;以单机模式启动nacos镜像。docker run -d --name nacos -p …

P5594 【XR-4】模拟赛————C++、Python

目录题目【XR-4】模拟赛题目描述输入格式输出格式样例 #1样例输入 #1样例输出 #1样例 #2样例输入 #2样例输出 #2样例 #3样例输入 #3样例输出 #3提示解题思路C的Code运行结果Python的代码运行结果题目 【XR-4】模拟赛 题目描述 X 校正在进行 CSP 前的校内集训。 一共有 nnn …

人工智能算法模型--Minimax(极大极小)搜索算法学习笔记

⬜⬜⬜ &#x1f430;&#x1f7e7;&#x1f7e8;&#x1f7e9;&#x1f7e6;&#x1f7ea; (*^▽^*)欢迎光临 &#x1f7e7;&#x1f7e8;&#x1f7e9;&#x1f7e6;&#x1f7ea;&#x1f430;⬜⬜⬜ ✏️write in front✏️ &#x1f4dd;个人主页&#xff1a;陈丹宇jmu &a…

基于MicroPython的ESP32在OLED上实时刷新温湿度

基于MicroPython的ESP32用起来后&#xff0c;做些小扩展功能&#xff1a; 1、通过DHT11获取温湿度&#xff1b; 2、将获取的温湿度显示在OLED上&#xff1b;并通过定时器&#xff0c;定时刷新&#xff1b; 3、OLED可显示中文&#xff1b; 一、DHT11获取温湿度 这个比较简单&a…

Java泛型详细内容讲解

1.什么是泛型 1.1背景&#xff1a; JAVA推出泛型以前&#xff0c;程序员可以构建一个元素类型为Object的集合&#xff0c;该集合能够存储任意的数据类型对象&#xff0c;而在使用该集合的过程中&#xff0c;需要程序员明确知道存储每个元素的数据类型&#xff0c;否则很容易引…

mysql中的实现字段或字符串拼接的三种方式

一、CONCAT函数concat函数是将多个字段或字符串拼接为一个字符串&#xff1b;但是字符串之间没有任何分隔。concat函数官方介绍-- CONCAT函数的语法如下&#xff1a; CONCAT(str1,str2,...)1.1、拼接非空字段或字符串SELECT CONCAT(字段1,字段2,字段3,...) from 表名;-- 拼接表…

C#构建Web服务项目实战(一)

概述本文通过VS2017&#xff0c;利用C#语言构建一个Web应用项目&#xff08;旧的ASP.NETWeb应用&#xff0c;非微软最新的.NETCore&#xff09;&#xff0c;并演示了如何利用Windows的IIS服务发布项目&#xff08;网站&#xff09;&#xff0c;以及在发布项目&#xff08;允许用…

C语言及算法设计课程实验三:最简单的C程序设计——顺序程序设计(三)

C语言及算法设计课程实验三&#xff1a;最简单的C程序设计——顺序程序设计&#xff08;三&#xff09;一、实验目的二、 实验内容2.3、计算存款利息三、 实验步骤3.3、顺序程序设计实验题目3&#xff1a;计算存款利息的实验步骤3.3.1、变量的声明3.3.2、变量的赋值3.3.3、计算…

递归分治时间复杂度主定理法

记录一下以前博客的证明过程&#xff0c;补充一下之前的结论 在算法导论中lgn一般指2为底的对数n&#xff0c;特此说明 以前写的博客记录了一下分治递归时间复杂度的结论&#xff0c;发现少了一个正则条件&#xff0c;而且也不覆盖所有的一般情况 https://blog.csdn.net/qq_1…