二叉树的层次遍历

This commit is contained in:
Yucheng Hu 2018-12-16 19:57:34 -05:00
parent 1fecd0a70a
commit bbacc57932
2 changed files with 124 additions and 24 deletions

View File

@ -290,28 +290,4 @@ public class LintcodeTest {
System.out.println(strSet.size());
}
/**
* 1480 https://www.lintcode.com/problem/dot-product/description
*/
@Test
public void test1480dotProduct() {
int[] A = { 1, 1, -1 };
int[] B = { 2147483647, 1, 3 };
int retStatus = 0;
// LENGTH CHECK
if (A.length == 0 || B.length == 0 || A.length != B.length)
retStatus = -1;
// ADDED
if (retStatus != -1) {
for (int i = 0; i < A.length; i++) {
retStatus = retStatus + A[i] * B[i];
}
}
System.out.println(retStatus);
}
}

View File

@ -0,0 +1,124 @@
package com.ossez.lang.tutorial.tests.lintcode;
import java.util.ArrayList;
import java.util.LinkedList;
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 LintCode0069SLevelOrderTest {
private final static Logger logger = LoggerFactory.getLogger(LintCode0069SLevelOrderTest.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]));
treeList.add(root);
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;
}
treeList.add(node);
}
// LEVEL
if (!isLeftChild) {
index++;
}
// MOVE TO RIGHT OR NEXT LEVEL
isLeftChild = !isLeftChild;
}
return root;
}
private List<List<Integer>> levelOrder(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<TreeNode>();
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();
list.add(curTN.val);
if (curTN.left != null) {
queue.offer(curTN.left);
}
if (curTN.right != null) {
queue.offer(curTN.right);
}
}
rs.add(list);
}
return rs;
}
}