When we make a structure for let's say a tree or a linked list, something of this nature:
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
What is the type for queue to store nodes of the structure? I thought it was a pointer to the struct.
std::queue<TreeNode*> q;
But if I try to initialize multiple nodes like this:
TreeNode* l = nullptr, r = nullptr;
It doesn't work and you have to do
TreeNode *l = nullptr, TreeNode *r = nullptr;
Can someone help me understand this a little better?
edit: After reading this answer Declaring multiple object pointers on one line causes compiler error What I'm understanding is that: In both C and C++, the * binds to the declarator, not the type specifier. In both languages, declarations are based on the types of expressions, not objects.
Then how is the queue declared the way it is?