MCPcopy Index your code
hub / github.com/krahets/LeetCode-Book / arrToTree

Method arrToTree

sword_for_offer/codes/java/include/TreeNode.java:23–45  ·  view source on GitHub ↗

Generate a binary tree with an array @param arr @return

(Integer[] arr)

Source from the content-addressed store, hash-verified

21 * @return
22 */
23 public static TreeNode arrToTree(Integer[] arr) {
24 TreeNode root = new TreeNode(arr[0]);
25 Queue<TreeNode> queue = new LinkedList<TreeNode>() {
26 {
27 add(root);
28 }
29 };
30 int i = 1;
31 while (!queue.isEmpty()) {
32 TreeNode node = queue.poll();
33 if (arr[i] != null) {
34 node.left = new TreeNode(arr[i]);
35 queue.add(node.left);
36 }
37 i++;
38 if (arr[i] != null) {
39 node.right = new TreeNode(arr[i]);
40 queue.add(node.right);
41 }
42 i++;
43 }
44 return root;
45 }
46
47 /**
48 * Serialize a binary tree to a list

Callers 15

mainMethod · 0.95
mainMethod · 0.95
mainMethod · 0.95
mainMethod · 0.95
mainMethod · 0.95
mainMethod · 0.95
mainMethod · 0.95
mainMethod · 0.95
mainMethod · 0.95
mainMethod · 0.95
mainMethod · 0.95
mainMethod · 0.95

Calls 1

addMethod · 0.45

Tested by

no test coverage detected