Question: Write a c++ program that: This is what I have: vector inorderTraversal(TreeNode *root) { vector result; stack s; const TreeNode *p = root; while (!s.empty()

Write a c++ program that:

This is what I have:

vector inorderTraversal(TreeNode *root)

{

vector result;

stack s;

const TreeNode *p = root;

while (!s.empty() || p != nullptr)

{

if (p != nullptr)

{

s.push(p);

p = p->left;

}

else

{

p = s.top();

s.pop();

result.push_back(p->value);

p = p->right;

}

}

return result;

}

Given a binary tree, return the inorder traversal of its nodes values using a stack. Given binary tree [1,null,2,3], return [1,3,2].

For my program, it only returns

Write a c++ program that: This is what I have: vector inorderTraversal(TreeNode

With this main:*root) { vector result; stack s; const TreeNode *p = root; while

1 2 Program ended with exit code

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!