美文网首页
69. Binary Tree Level Order Trav

69. Binary Tree Level Order Trav

作者: 鸭蛋蛋_8441 | 来源:发表于2019-05-28 10:21 被阅读0次

Description

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

The first data is the root node, followed by the value of the left and right son nodes, and "#" indicates that there is no child node.

The number of nodes does not exceed 20.

Example

Example 1:

Input:{1,2,3}

Output:[[1],[2,3]]

Explanation:

  1

/ \

2  3

it will be serialized {1,2,3}

level order traversal

Example 2:

Input:{1,#,2,3}

Output:[[1],[2],[3]]

Explanation:

1

\

  2

/

3

it will be serialized {1,#,2,3}

level order traversal

Challenge

Challenge 1: Using only 1 queue to implement it.

Challenge 2: Use BFS algorithm to do it.

思路:

有两种解法, 以一是用一个队列, 二是用两个队列交换

代码:

用1个队列的

用两个队列的:

相关文章

网友评论

      本文标题:69. Binary Tree Level Order Trav

      本文链接:https://www.haomeiwen.com/subject/qghrtctx.html