The following code is in C++ I am encountering the error of value is not usable in a constant expression error
int sumNumbers(TreeNode* root) {
stack<pair<TreeNode*, int>> st;
st.push(make_pair(root, root->val));
int sum = 0;
while(!st.empty()){
pair<TreeNode*, int> temp = st.top();
st.pop();
TreeNode* node = temp.first;
int value = temp.second;
if(node->left==NULL && node->right==NULL){
sum += value;
}
if(node->left){
st.push(pair< node->left, value*10 + node->left->val >);
}
if(node->right){
st.push(pair< node->right, value*10 + node->right->val >);
}
}
return sum;
}
The error is in the line:
if(node->left){
st.push(pair< node->left, value*10 + node->left->val >);
}
The error is:
Line 29: Char 37: error: the value of 'node' is not usable in a constant expression st.push(pair< node->left, value*10 + node->left->val >);
I am not able to figure out why this error is encountered here?
make_pair()if(auto nl = node->left) { st.push(make_pair< nl, value*10 + nl->val >);. Anifstatement may define a local variable for the value being compared.