美联航的积分是只要转个一万进去就可以把所有的有效期都延长吗# Money - 海外理财
w*u
1 楼
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-
inorder-traversal/
————————————————————————————————————
我的解法
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder,
int inorderSize) {
if (preorderSize == 0) return NULL;
int rootVal = preorder[0];
int rootNodePos = -1;
for (int i = 0; i < inorderSize; i++) {
if (rootVal == inorder[i]) rootNodePos = i;
}
struct TreeNode *root = malloc(sizeof(struct TreeNode));
root->val = rootVal;
root->left = buildTree(preorder + 1, rootNodePos, inorder, rootNodePos);
root->right = buildTree(preorder + rootNodePos + 1, preorderSize -
rootNodePos - 1, inorder + rootNodePos + 1, inorderSize - rootNodePos - 1);
}
在leetcode上run了一下,要20ms,不是最优解。请问大虾们这道题如何得到最优解。谢
谢!!!
Note:
You may assume that duplicates do not exist in the tree.
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-
inorder-traversal/
————————————————————————————————————
我的解法
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder,
int inorderSize) {
if (preorderSize == 0) return NULL;
int rootVal = preorder[0];
int rootNodePos = -1;
for (int i = 0; i < inorderSize; i++) {
if (rootVal == inorder[i]) rootNodePos = i;
}
struct TreeNode *root = malloc(sizeof(struct TreeNode));
root->val = rootVal;
root->left = buildTree(preorder + 1, rootNodePos, inorder, rootNodePos);
root->right = buildTree(preorder + rootNodePos + 1, preorderSize -
rootNodePos - 1, inorder + rootNodePos + 1, inorderSize - rootNodePos - 1);
}
在leetcode上run了一下,要20ms,不是最优解。请问大虾们这道题如何得到最优解。谢
谢!!!