Title: 第 63 期(数据结构-队列):队列的应用 · Issue #66 · fezaoduke/fe-practice-hard · GitHub
Open Graph Title: 第 63 期(数据结构-队列):队列的应用 · Issue #66 · fezaoduke/fe-practice-hard
X Title: 第 63 期(数据结构-队列):队列的应用 · Issue #66 · fezaoduke/fe-practice-hard
Description: 上一期介绍了 队列和队列的模拟,这一期来看看队列的应用。 这是一道经典的面试题,要求不使用递归广度遍历下面的二叉树: 要求在控制台依次输出遍历结果:1, 2, 3, 4, 5, 6, 7, 8 测试数据: { "value": 1, "left": { "value": 2, "left": { "value": 4 } }, "right": { "value": 3, "left": { "value": 5, "left": { "value": 7 }, "ri...
Open Graph Description: 上一期介绍了 队列和队列的模拟,这一期来看看队列的应用。 这是一道经典的面试题,要求不使用递归广度遍历下面的二叉树: 要求在控制台依次输出遍历结果:1, 2, 3, 4, 5, 6, 7, 8 测试数据: { "value": 1, "left": { "value": 2, "left": { "value": 4 } }, "right": { "value": 3, "left": {...
X Description: 上一期介绍了 队列和队列的模拟,这一期来看看队列的应用。 这是一道经典的面试题,要求不使用递归广度遍历下面的二叉树: 要求在控制台依次输出遍历结果:1, 2, 3, 4, 5, 6, 7, 8 测试数据: { "value": 1, "left": { "value": 2, "left": { "va...
Opengraph URL: https://github.com/fezaoduke/fe-practice-hard/issues/66
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"第 63 期(数据结构-队列):队列的应用","articleBody":"上一期介绍了 [队列和队列的模拟](https://github.com/fezaoduke/fe-practice-hard/issues/65),这一期来看看队列的应用。\r\n\r\n这是一道经典的面试题,要求不使用递归广度遍历下面的二叉树:\r\n\r\n\u003e 要求在控制台依次输出遍历结果:1, 2, 3, 4, 5, 6, 7, 8\r\n\r\n**测试数据:**\r\n```json\r\n{\r\n \"value\": 1,\r\n \"left\": {\r\n \"value\": 2,\r\n \"left\": {\r\n \"value\": 4\r\n }\r\n },\r\n \"right\": {\r\n \"value\": 3,\r\n \"left\": {\r\n \"value\": 5,\r\n \"left\": {\r\n \"value\": 7\r\n },\r\n \"right\": {\r\n \"value\": 8\r\n }\r\n },\r\n \"right\": {\r\n \"value\": 6\r\n }\r\n }\r\n}\r\n```\r\n\r\n**思路:**\r\n\r\n广度遍历的特点是从根节点开始,沿着树的宽度一层一层进行遍历,直到所有节点都被遍历完为止,我们可以利用队列来实现这个算法。\r\n\r\n核心思路是用一个队列来临时保存节点。遍历开始时先将根节点加入队列,然后从队列中取出第一个元素(此时是根节点),同时将该节点的子节点(如有)加入队列,以此循环操作,直至队列为空。参照下图示意(点击可查看大图):\r\n\r\n\r\n\r\n**上码:**\r\n\r\n```js\r\nfunction levelOrderTraversal(tree) {\r\n var queue = []; // 使用数组模拟队列\r\n\r\n queue.push(tree); // 根节点入队列\r\n while (queue.length !== 0) {\r\n tree = queue.shift(); // 取出队列第一个节点\r\n\r\n console.log(tree.value);\r\n\r\n if (tree.left) {\r\n queue.push(tree.left); // 左子节点入队列\r\n }\r\n if (tree.right) {\r\n queue.push(tree.right); // 右子节点入队列\r\n }\r\n }\r\n}\r\n```\r\n","author":{"url":"https://github.com/wingmeng","@type":"Person","name":"wingmeng"},"datePublished":"2019-07-19T14:26:24.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/66/fe-practice-hard/issues/66"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:6521503e-e138-bf55-62a4-e2ef8bbb2a3b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CC3C:6E8AA:39A0D3C:4BB12C0:6992DFDA |
| html-safe-nonce | fd2980860e61476a70e0573dd4e5bbb5a6b9bd371564bffbc9cbfd3dde90f73d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQzNDOjZFOEFBOjM5QTBEM0M6NEJCMTJDMDo2OTkyREZEQSIsInZpc2l0b3JfaWQiOiI1NDM4MjkwNDQ4MTE5Njg1MDgyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 2334ea2f0ca95dfd18aca0cde71bcddcf648a96aeab8985b9b3f538ec391900a |
| hovercard-subject-tag | issue:470354412 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/fezaoduke/fe-practice-hard/66/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c2efdced5e196f960f5b6c179ff187a60352cca9700d738b2ba311bab3803eea/fezaoduke/fe-practice-hard/issues/66 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c2efdced5e196f960f5b6c179ff187a60352cca9700d738b2ba311bab3803eea/fezaoduke/fe-practice-hard/issues/66 |
| og:image:alt | 上一期介绍了 队列和队列的模拟,这一期来看看队列的应用。 这是一道经典的面试题,要求不使用递归广度遍历下面的二叉树: 要求在控制台依次输出遍历结果:1, 2, 3, 4, 5, 6, 7, 8 测试数据: { "value": 1, "left": { "value": 2, "left": { "value": 4 } }, "right": { "value": 3, "left": {... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | wingmeng |
| hostname | github.com |
| expected-hostname | github.com |
| None | 42c603b9d642c4a9065a51770f75e5e27132fef0e858607f5c9cb7e422831a7b |
| turbo-cache-control | no-preview |
| go-import | github.com/fezaoduke/fe-practice-hard git https://github.com/fezaoduke/fe-practice-hard.git |
| octolytics-dimension-user_id | 21255532 |
| octolytics-dimension-user_login | fezaoduke |
| octolytics-dimension-repository_id | 184997815 |
| octolytics-dimension-repository_nwo | fezaoduke/fe-practice-hard |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 184997815 |
| octolytics-dimension-repository_network_root_nwo | fezaoduke/fe-practice-hard |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 84dcb133269e3cfe6e0296cc85fbacb92cae92bb |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width