[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
- Binary Tree Level Order Traversal [LEETCODE]LeetCode
- 102-Binary Tree Level Order Traversal
- 107-Binary Tree Level Order Traversal II
- 103. Binary Tree Zigzag Level Order Traversal
- [LintCode] Check Full Binary Tree
- Traversals of binary tree
- Leetcode Binary Tree PathsLeetCode
- 257-Binary Tree Paths
- 543-Diameter of Binary Tree
- 563-Binary Tree Tilt
- 655-Print Binary Tree
- 654-Maximum Binary Tree
- 814-Binary Tree Pruning
- 110-Balanced Binary Tree
- 545. Boundary of Binary Tree
- 257. Binary Tree Paths
- Construct String from Binary TreeStruct
- 226-Invert Binary Tree
- [LeetCode] 226. Invert Binary TreeLeetCode
- [LeetCode] 543. Diameter of Binary TreeLeetCode
- 111-Minimum Depth of Binary Tree
- 662-Maximum Width of Binary Tree
- 104. Maximum Depth of Binary Tree
- LeetCode 543. Diameter of Binary TreeLeetCode
- LeetCode545.Boundary-of-Binary-TreeLeetCode
- Leetcode 226. Invert Binary TreeLeetCode
- [leetcode]binary-tree-inorder-traversalLeetCode
- [leetcode]maximum-depth-of-binary-treeLeetCode
- LeetCode之Univalued Binary Tree(Kotlin)LeetCodeKotlin
- LeetCode之Binary Tree Pruning(Kotlin)LeetCodeKotlin
- 104-Maximum Depth of Binary Tree
- 637-Average of Levels in Binary Tree
- 669-Trim a Binary Search Tree
- 662. Maximum Width of Binary Tree
- 173. Binary Search Tree Iterator
- LeetCode#110.Balanced Binary Tree(Tree/Height/DFS/Recursion)LeetCode
- LeetCode之Increasing Order Search Tree(Kotlin)LeetCodeKotlin