69. Binary Tree Level Order Trav

2019-05-28  本文已影响0人  鸭蛋蛋_8441

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个队列的

用两个队列的:

上一篇 下一篇

猜你喜欢

热点阅读