We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
解题思想: 在广度优先遍历过程中,如果遇到叶子节点,停止遍历,返回节点层级。
var minDepth = function(root) { if (!root) return 0; // 广度优先需要使用一个队列,同时额外再维护一个深度 const q = [[root, 1]]; while (q.length) { const [n, deepth] = q.shift(); if (!n.left && !n.right) return deepth; // console.log(n.val, deepth); if (n.left) q.push([n.left, deepth + 1]); if (n.right) q.push([n.right, deepth + 1]); } };
复杂度分析:
The text was updated successfully, but these errors were encountered:
No branches or pull requests
方法一:广度优先遍历
解题思想:
在广度优先遍历过程中,如果遇到叶子节点,停止遍历,返回节点层级。
代码:
复杂度分析:
The text was updated successfully, but these errors were encountered: