relocation搬家公司的问题# Living
e*s
1 楼
一个简单的construct BT with preorder and inorder.
这是1337code大哥的C++ code(递归部分).
root->left = buildInorderPreorder(in, pre+1, i, offset);
root->right = buildInorderPreorder(in+i+1, pre+i+1, n-i-1, offset+i+1);
但是在C#中不在unsafe block里面不能用指针(我也不想用指针)。
有什么方法能写得这么简洁呢?
int[] linorder = new int[divider];
int[] lpreorder = new int[divider];
int[] rinorder = new int[n - divider - 1];
int[] rpreorder = new int[n - divider - 1];
for(int i = 0; i < divider; i++)
linorder[i] = inorder[i];
for(int i = 1; i <= divider; i++)
lpreorder[i-1] = preorder[i];
for (int i = divider + 1; i < n; i++)
rinorder[i - (divider + 1)] = inorder[i];
for (int i = 1 + divider; i < n; i++)
rpreorder[i - (1 + divider)] = preorder[i];
root.left = buildinorderpreorder(linorder, lpreorder);
root.right = buildinorderpreorder(rinorder, rpreorder);
这是我的code, 建立left inorder, left preorder, right inorder, right
preorder,再放回去递归,觉得很不简洁。
这是1337code大哥的C++ code(递归部分).
root->left = buildInorderPreorder(in, pre+1, i, offset);
root->right = buildInorderPreorder(in+i+1, pre+i+1, n-i-1, offset+i+1);
但是在C#中不在unsafe block里面不能用指针(我也不想用指针)。
有什么方法能写得这么简洁呢?
int[] linorder = new int[divider];
int[] lpreorder = new int[divider];
int[] rinorder = new int[n - divider - 1];
int[] rpreorder = new int[n - divider - 1];
for(int i = 0; i < divider; i++)
linorder[i] = inorder[i];
for(int i = 1; i <= divider; i++)
lpreorder[i-1] = preorder[i];
for (int i = divider + 1; i < n; i++)
rinorder[i - (divider + 1)] = inorder[i];
for (int i = 1 + divider; i < n; i++)
rpreorder[i - (1 + divider)] = preorder[i];
root.left = buildinorderpreorder(linorder, lpreorder);
root.right = buildinorderpreorder(rinorder, rpreorder);
这是我的code, 建立left inorder, left preorder, right inorder, right
preorder,再放回去递归,觉得很不简洁。