int next() { if (!hasNext()) { throw NoSuchElementException("All nodes have been visited!"); } TreeNode res = stack.pop(); if (!stack.isEmpty()) { TreeNode top = stack.peek(); if (res == top->left) { findNextLeaf(top->right); // find next leaf in right sub-tree } } return res->val; }
/** Constructor */ PostOrderBinaryTreeIterator(TreeNode *root) { findNextLeaf(root); }
PostorderIterator(TreeNode* root){ findNextLeaf(root); }