Inorder Traversal of Cartesian Tree
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
TreeNode* helper(vector<int>& A, int l, int r) { if(l > r) return NULL; if(l == r) return new TreeNode(A[l]); int ma = max_element(begin(A) + l, begin(A) + r + 1) - begin(A); TreeNode* res = new TreeNode(A[ma]); res->left = helper(A,l,ma - 1); res->right = helper(A,ma + 1,r); return res; } TreeNode* Solution::buildTree(vector<int> &A) { return helper(A, 0, A.size() - 1); }
|