二叉树的最小深度不适用于所有测试用例

Minimum depth of a binary tree is not working for all test cases

我正在尝试找到二叉树的最小深度;但是,示例 5 中的测试用例失败了。我不确定我的逻辑是否存在缺陷,无法使它适用于所有测试用例。我正在做的一个例子如下:

Example:
Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its minimum:
depth = 2

我有以下代码来完成这个:

class TreeNode {
  constructor(val) {
    this.val = val;
    this.left = this.right = null;
  }
}

const minDepth = root => {
  if (!root) return 0

  const traverse = root => {
    let counter = 1
    if (!root) return counter
    let current
    let queue = [root, 's']

    while (queue.length > 1) {
      current = queue.shift()
      if (current === 's') counter++, queue.push('s')
      if (!current.left && !current.right) return counter
      else {
        if (current.left) queue.push(current.left)
        if (current.right) queue.push(current.right)
      }
    }
    return counter
  }
  return root.left && root.right ? Math.min(traverse(root.left), traverse(root.right)) + 1 : traverse(root)
}

//example 1  
const tree1 = new TreeNode(3)
tree1.left = new TreeNode(9)
tree1.right = new TreeNode(20)
tree1.right.left = new TreeNode(15)
tree1.right.right = new TreeNode(7)

//example 2
const tree2 = new TreeNode(1)
tree2.left = new TreeNode(2)
tree2.right = new TreeNode(3)
tree2.left.left = new TreeNode(4)
tree2.right.right = new TreeNode(5)

//example 3
const tree3 = new TreeNode(0)

//example 4
const tree4 = new TreeNode(1)
tree4.left = new TreeNode(2)

//example 5 not working
const tree5 = new TreeNode(1)
tree5.left = new TreeNode(2)
tree5.left.right = new TreeNode(3)
tree5.left.right.right = new TreeNode(4)
tree5.left.right.right.right = new TreeNode(5)


console.log(minDepth(tree1))
console.log(minDepth(tree2))
console.log(minDepth(tree3))
console.log(minDepth(tree4))
console.log(minDepth(tree5))

关于我遗漏了什么有什么想法吗?

我不太确定你在这里的整体方法。您的函数似乎设置为执行递归,但随后您在嵌套函数中迭代工作。这两种方法对我来说都有意义(递归似乎更容易),但我建议明确地致力于其中一种方法。

如果您选择迭代,基本上您会 运行 一个 BFS 并在您到达第一个叶节点时停止。叶节点是没有子节点的节点,我们也需要检测这种情况以进行递归。

递归简单地将 1 传递给每个叶子的父级。否则,当前节点为内部节点;为它加 1 并传递其子级递归的最小值(通过合并无限值忽略任何不存在的)。

const minDepth = root => {
  if (!root) {
    return 0;
  }
  else if (!root.left && !root.right) {
    return 1;
  }
  
  return 1 + Math.min(minDepth(root.left) || Infinity,
                      minDepth(root.right) || Infinity);
};

class TreeNode {
  constructor(val) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

/*
    3
   / \
  9   20
      / \
     15   7
     
     should be 2
*/
const tree1 = new TreeNode(3);
tree1.left = new TreeNode(9);
tree1.right = new TreeNode(20);
tree1.right.left = new TreeNode(15);
tree1.right.right = new TreeNode(7);

/*
     1
    / \
   2   3
  /     \
 4       5
 
  should be 3
*/
const tree2 = new TreeNode(1);
tree2.left = new TreeNode(2);
tree2.right = new TreeNode(3);
tree2.left.left = new TreeNode(4);
tree2.right.right = new TreeNode(5);

/*
  0
  
   should be 1
*/
const tree3 = new TreeNode(0);

/*
   1
  /
 2
 
  should be 2
*/
const tree4 = new TreeNode(1);
tree4.left = new TreeNode(2);

/*
       1
      /
     2
      \ 
       3
        \
         4
          \
           5
           
           should be 5
*/
const tree5 = new TreeNode(1);
tree5.left = new TreeNode(2);
tree5.left.right = new TreeNode(3);
tree5.left.right.right = new TreeNode(4);
tree5.left.right.right.right = new TreeNode(5);

console.log(minDepth(tree1));
console.log(minDepth(tree2));
console.log(minDepth(tree3));
console.log(minDepth(tree4));
console.log(minDepth(tree5));

这是一个 BFS 版本:

const minDepth = root => {
  for (const queue = [[root, 1]]; queue.length;) {
    const [node, depth] = queue.shift();
    
    if (node) {
      if (!node.left && !node.right) {
        return depth;
      }
      
      queue.push([node.left, depth + 1], [node.right, depth + 1]);
    }
  }

  return 0;
};

class TreeNode {
  constructor(val) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

/*
    3
   / \
  9   20
      / \
     15   7
     
     should be 2
*/
const tree1 = new TreeNode(3);
tree1.left = new TreeNode(9);
tree1.right = new TreeNode(20);
tree1.right.left = new TreeNode(15);
tree1.right.right = new TreeNode(7);

/*
     1
    / \
   2   3
  /     \
 4       5
 
  should be 3
*/
const tree2 = new TreeNode(1);
tree2.left = new TreeNode(2);
tree2.right = new TreeNode(3);
tree2.left.left = new TreeNode(4);
tree2.right.right = new TreeNode(5);

/*
  0
  
   should be 1
*/
const tree3 = new TreeNode(0);

/*
   1
  /
 2
 
  should be 2
*/
const tree4 = new TreeNode(1);
tree4.left = new TreeNode(2);

/*
       1
      /
     2
      \ 
       3
        \
         4
          \
           5
           
           should be 5
*/
const tree5 = new TreeNode(1);
tree5.left = new TreeNode(2);
tree5.left.right = new TreeNode(3);
tree5.left.right.right = new TreeNode(4);
tree5.left.right.right.right = new TreeNode(5);

console.log(minDepth(tree1));
console.log(minDepth(tree2));
console.log(minDepth(tree3));
console.log(minDepth(tree4));
console.log(minDepth(tree5));