c++ - Running on my laptop, but giving runtime error on Leetcode -
i have build tree preorder , inorder traversals. code working fine on laptop, not on leetcode. giving runtime error, suggestions ?
int findindex(vector<int>& inorder,int index) { int size = inorder.size(); for(int i=0;i<size;i++) { if(index == inorder[i]) return index; } } treenode* helper(vector<int>& preorder, vector<int>& inorder, int start, int end) { static int preindex = 0; if(start > end) return null; treenode* head; head = (treenode *)malloc(sizeof(treenode)); head->val = preorder[preindex++]; head->left = null; head->right = null; if(start == end) return head; int index = findindex(inorder,head->val); head->left = helper(preorder, inorder,start, index-1); head ->right = helper(preorder, inorder, index+1, end); return head; } treenode* solution::buildtree(vector<int> &preorder, vector<int> &inorder) { int size = preorder.size(); if(size == 0) return null; return helper(preorder, inorder, 0, size-1); }
Comments
Post a Comment