Create an iterator to traverse a binary tree. When the next function is called on the binary tree return the value at the next node as if you are doing an inorder traversal of the tree. Restrictions: Nodes do not have pointers to their parent node and you can't use recursion.
Utilisateur anonyme
class TreeNode { public: int val; TreeNode *left; TreeNode *right; TreeNode(int val) : val(val), left(NULL), right(NULL) {} }; class TreeIterator { private: stack<div>stackA; public: TreeIterator(TreeNode *root = NULL){ while (root){ stackA.push(root); root = root->left; } } TreeNode* getNext(){ if (stackA.empty()) return NULL; TreeNode *target = stackA.top(); stackA.pop(); TreeNode *node = target->right; while (node){ stackA.push(node); node = node->left; } return target; } };</div>