7题目描述：

Java:

``````public TreeNode buildTree(int[] preorder, int[] inorder) {
int length = preorder.length;
if(length == 0) {
return null;
}
int rootValue = preorder[0];
int rootIndex = 0;
for(int i = 0; i < length; i++) {
if(inorder[i] == rootValue) {
rootIndex = i;
break;
}
}
TreeNode root = new TreeNode(rootValue);
root.left = buildTree(Arrays.copyOfRange(preorder, 1, rootIndex + 1),
Arrays.copyOfRange(inorder, 0, rootIndex + 1));
root.right = buildTree(Arrays.copyOfRange(preorder, rootIndex + 1, length),
Arrays.copyOfRange(inorder, rootIndex + 1, length));
return root;
}
``````

JavaScript:

``````var buildTree = function(preorder, inorder) {
if(preorder.length === 0) {
return null;
}
const tree = new TreeNode(preorder[0]);

let inorderRootIndex = inorder.indexOf(tree.val);
let preorderLeft = preorder.slice(1, inorderRootIndex + 1);
let inorderLeft = inorder.slice(0, inorderRootIndex);
tree.left = buildTree(preorderLeft, inorderLeft);

let preorderRight = preorder.slice(inorderRootIndex + 1);
let inorderRight = inorder.slice(inorderRootIndex + 1);
tree.right = buildTree(preorderRight, inorderRight);

return tree;
};
``````

16题目描述：

1.快速幂

pow(2, 10)
pow(2 * 2, 5)
pow(2, 4) * 2
pow(2, 2) * pow(2, 2)
pow(2, 1) * pow(2, 1)

Java:

``````public double myPow(double x, int n) {
if(n == 0) {
return 1;
}
if(n == 1) {
return x;
}
if(n == -1) {
return 1 / x;
}
if(n % 2 != 0) {
return x * myPow(x, n - 1);
}
return myPow(x * x, n / 2);
}
``````

JavaScript:

``````var myPow = function(x, n) {
if(n === 0) {
return 1;
}
if(n === 1) {
return x;
}
if(n < 0) {
return 1 / myPow(x, -n);
}
if(n % 2 !== 0) {
return x * myPow(x, n - 1);
}
return myPow(x * x, n / 2);
};
``````