In my opinion, the complexity of the space is O (1), because after each frame of the stack popped up, a reference to the object created on this frame of the stack is lost and garbage. I'm right?
No, you are mistaken in assuming this. Ray Toal explained this very beautifully. At any point in the recursion, you must count all of these internal stack frames that are stored in memory, since Space-Complexity takes into account all the auxiliary space (extra space) along with input (not specified here).
Further
I am looking for the spatial complexity of the objects 'TreeData' created.
The maximum space consumed by the recursive program is proportional to the maximum depth of the recursive tree. The maximum depth of a recursion tree is defined as the length of the longest path in the tree.
For this program, the program will start from the root of the tree and first start creating the left subtree, and then check the right subtree. Finally, it will return the length of the longest path and whether the tree will be balanced OR not.
It seems to me that at any moment there will be only 3 TreeData objects. This is what I want to check.
No, at any specific runtime, all the left subtrees are checked first, and then the right subtrees are checked, so the number of TreeData objects in memory will be only 1. It will check it to the left each time OR the right child. And, therefore, all of those (log n --- in the case of a balanced tree, OR n --- in the case of a degenerate tree), but the nodes, except one, will be stored in memory. At a certain point in time, only one TreeData object will be in an active state, others will be suspended and stored in memory and waiting for their turn to pop up.