# [LintCode] Binary Tree Level Order Traversal（二叉树的层次遍历）

```  3
/ \
9  20
/  \
15   7```

```[
[3],
[9,20],
[15,7]
]```

## 代码

GitHub 的源代码，请访问下面的链接：

https://github.com/cwiki-us/java-tutorial/blob/master/src/test/java/com/ossez/lang/tutorial/tests/lintcode/LintCode0069LevelOrderTest.java

```package com.ossez.lang.tutorial.tests.lintcode;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ossez.lang.tutorial.models.TreeNode;

/**
* <p>
* 69
* <ul>
* <li>@see <a href=
* "https://www.cwiki.us/display/ITCLASSIFICATION/Binary+Tree+Level+Order+Traversal">https://www.cwiki.us/display/ITCLASSIFICATION/Binary+Tree+Level+Order+Traversal</a>
* <li>@see<a href=
* "https://www.lintcode.com/problem/binary-tree-level-order-traversal">https://www.lintcode.com/problem/binary-tree-level-order-traversal</a>
* </ul>
* </p>
*
* @author YuCheng
*
*/
public class LintCode0069LevelOrderTest {

private final static Logger logger = LoggerFactory.getLogger(LintCode0069LevelOrderTest.class);

/**
*
*/
@Test
public void testMain() {
logger.debug("BEGIN");
String data = "{3,9,20,#,#,15,7}";

TreeNode tn = deserialize(data);
System.out.println(levelOrder(tn));

}

/**
* Deserialize from array to tree
*
* @param data
* @return
*/
private TreeNode deserialize(String data) {
// NULL CHECK
if (data.equals("{}")) {
return null;
}

ArrayList<TreeNode> treeList = new ArrayList<TreeNode>();

data = data.replace("{", "");
data = data.replace("}", "");
String[] vals = data.split(",");

// INSERT ROOT
TreeNode root = new TreeNode(Integer.parseInt(vals[0]));

int index = 0;
boolean isLeftChild = true;
for (int i = 1; i < vals.length; i++) {
if (!vals[i].equals("#")) {
TreeNode node = new TreeNode(Integer.parseInt(vals[i]));
if (isLeftChild) {
treeList.get(index).left = node;
} else {
treeList.get(index).right = node;
}
}

// LEVEL
if (!isLeftChild) {
index++;
}

// MOVE TO RIGHT OR NEXT LEVEL
isLeftChild = !isLeftChild;
}

return root;

}

private List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> rs = new ArrayList<List<Integer>>();

// NULL CHECK
if (root == null) {
return rs;
}

queue.offer(root);

while (!queue.isEmpty()) {
int length = queue.size();
List<Integer> list = new ArrayList<Integer>();

for (int i = 0; i < length; i++) {
TreeNode curTN = queue.poll();
if (curTN.left != null) {
queue.offer(curTN.left);
}
if (curTN.right != null) {
queue.offer(curTN.right);
}
}

}

return rs;
}
}```

571 篇文章30 人订阅

0 条评论

## 相关文章

### activemq的高可用(zookeeper+leveldb)主从集群

ActiveMQ 是Apache出品，最流行的，能力强劲的开源消息总线。完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现

33630

### OtterCTF 13道内存取证题目详细解析（下）

The reason that we took rick's PC memory dump is because there was a malware inf...

65750

### 面试|return 和finally那些事儿

try/catch/finally语句块的finally和return谁先执行呢？也即是我们在try内部调用return，然后finally内部又去修改retu...

13540

### 「小程序JAVA实战」swagger2的使用与接口测试（34）

import org.springframework.boot.SpringApplication; import org.springframework.bo...

19620

### Java微基准测试框架JMH

JMH，即Java Microbenchmark Harness，这是专门用于进行代码的微基准测试的一套工具API。

17930

### OtterCTF 13道内存取证题目详细解析（中）

From a little research we found that the username of the logged on character is ...

27030

32760

15640

21010

44930