[LintCode] Binary Tree Level Order
描述
給出一棵二叉樹,返回其節點值的層次遍歷(逐層從左往右訪問)
樣例
給一棵二叉樹 {3,9,20,#,#,15,7} :
3 / 9 20 / 15 7
返回他的分層遍歷結果:
[ [3], [9,20], [15,7] ]
挑戰
挑戰1:只使用一個佇列去實現它
挑戰2:用BFS演算法來做
程式碼
GitHub 的原始碼,請訪問下面的連結:
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;
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));
}
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;
}
}
點評
這個程式可以使用佇列的廣度優先演算法來進行遍歷。
需要注意的是,因為在輸出結果的時候需要按照層級來進行輸出,那麼需要考慮的一個演算法就是二叉樹的層級遍歷演算法。
這個演算法要求在遍歷的時候記錄樹的層級。
©著作權歸作者所有:來自51CTO部落格作者HoneyMoose的原創作品,如需轉載,請註明出處,否則將追究法律責任
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/818/viewspace-2819200/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Binary Tree Level Order Traversal [LEETCODE]LeetCode
- Leetcode Binary Tree Level Order TraversalLeetCode
- Leetcode Binary Tree Level Order Traversal IILeetCode
- Leetcode-Binary Tree Level Order TraversalLeetCode
- Binary Tree Level Order Traversal leetcode javaLeetCodeJava
- Leetcode Binary Tree Zigzag Level Order TraversalLeetCode
- Leetcode-Binary Tree Level Order Traversal IILeetCode
- Binary Tree Level Order Traversal II leetcode javaLeetCodeJava
- Leetcode-binary Tree Zigzag Level Order TraversalLeetCode
- Binary Tree ZigZag Level Order Traversal leetcode javaLeetCodeJava
- leetcode102:Binary Tree Level Order TraversalLeetCode
- [LeetCode]107. Binary Tree Level Order Traversal IILeetCode
- [LintCode] Check Full Binary Tree
- LeetCode解題報告 102. Binary Tree Level Order Traversal [easy]LeetCode
- 【LeetCode從零單排】No102 Binary Tree Level Order TraversalLeetCode
- LintCode-Serialization and Deserialization Of Binary Tree
- LintCode-Implement Iterator of Binary Search Tree
- LintCode-Search Range in Binary Search Tree
- LintCode-Remove node in Binary Search TreeREM
- LeetCode-Binary Tree Vertical Order TraversalLeetCode
- Lintcode-Max Tree
- Leetcode Binary Tree PathsLeetCode
- 173. Binary Search Tree Iterator
- LeetCode Invert Binary TreeLeetCode
- Leetcode Balanced Binary TreeLeetCode
- Order-level sales credit total () must equal 100%.
- Construct String from Binary TreeStruct
- LeetCode-Binary Tree PathsLeetCode
- leetcode - Binary Tree Preorder TraversalLeetCode
- Leetcode Binary Tree Inorder TraversalLeetCode
- Leetcode Binary Tree Preorder TraversalLeetCode
- Leetcode Binary Tree Postorder TraversalLeetCode
- Leetcode Maximum Depth of Binary TreeLeetCode
- Leetcode-Balanced Binary TreeLeetCode
- Balanced Binary Tree leetcode javaLeetCodeJava
- LeetCode545.Boundary-of-Binary-TreeLeetCode
- [leetcode]maximum-depth-of-binary-treeLeetCode
- [leetcode]binary-tree-inorder-traversalLeetCode