Question

c++ write a iterative post order traversal using 1 stack and (passing a Node pointer to...

c++ write a iterative post order traversal using 1 stack and (passing a Node pointer to the root of the binary search to the parameter).

Homework Answers

Answer #1

Sol.

We can implement function of post order traversal using only 1 stack.

def postorderTraversal(self, root: TreeNode) -> List[int]:
retu = [] // Empty List
if not root: return ret // Return list if root is null
st = [root] * 2 // push the root in the stack 2 times
while st: // while stack not empty
cur = st.pop() // save stack top in cur variable
if st and st[-1] is cur: // if stack is empty and stack top is cur oherwise goto else part (push cur.val in list ret)
if cur.right: // if current right is not null
st += [cur.right] * 2 //push 2 times cur.left in stack
if cur.left: // if current left is not null
st += [cur.left] * 2 //push 2 times cur.right in stack
else:
ret.append(cur.val)
return ret // finally return list ret.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In this lab, you will write a program that creates a binary search tree based on...
In this lab, you will write a program that creates a binary search tree based on user input. Then, the user will indicate what order to print the values in. **Please write in C code** Start with the bst.h and bst.c base code provided to you. You will need to modify the source and header file to complete this lab. bst.h: #ifndef BST_H #define BST_H typedef struct BSTNode { int value; struct BSTNode* left; struct BSTNode* right; } BSTNode; BSTNode*...
1) Write a sequence of C++ statements to add a node to the front [1] of...
1) Write a sequence of C++ statements to add a node to the front [1] of the list. Make sure you put 4 in the new Node. 2) Write a while loop here that goes through the list until P points to the node right before the rear node. [2] P = Front; // to start with 3) Write a for loop here that makes P stop at the (J-1)th node. [2] Think how many times the pointer needs to...
USE C++ Write a function named find that takes a pointer to the beginning and a...
USE C++ Write a function named find that takes a pointer to the beginning and a pointer to the end (1 element past the last) of an array, as well as a value. The function should search for the given value and return a pointer to the first element with that value, or the end pointer if no element was found.
Create an add method for the BST (Binary Search Tree) class. add(self, new_value: object) -> None:...
Create an add method for the BST (Binary Search Tree) class. add(self, new_value: object) -> None: """This method adds new value to the tree, maintaining BST property. Duplicates must be allowed and placed in the right subtree.""" Example #1: tree = BST() print(tree) tree.add(10) tree.add(15) tree.add(5) print(tree) tree.add(15) tree.add(15) print(tree) tree.add(5) print(tree) Output: TREE in order { } TREE in order { 5, 10, 15 } TREE in order { 5, 10, 15, 15, 15 } TREE in order {...
C++ Using the Stack operations, write a pseudocode routine, dupA, that takes aStack for string, checks...
C++ Using the Stack operations, write a pseudocode routine, dupA, that takes aStack for string, checks to see if the top starts with ‘A’ or ‘a’. If so, duplicate the top of the stack (i.e. pop a copy of that value onto the stack) else if length > 10, pop it off the stack
c++ 1.using a balanced search tree Given (a b* (c d* e)) draw the corresponding tree....
c++ 1.using a balanced search tree Given (a b* (c d* e)) draw the corresponding tree. - Indicates non-leaves. - Indicate the balance factor and height for each non-leaf. 2. To compute and store the height and balance factor of each vertex, what traversal order would be ideal? Why?
LANGUAGE IS C++ 1- A link based getEntry method requires how many steps to access the...
LANGUAGE IS C++ 1- A link based getEntry method requires how many steps to access the nth item in the list? a)n b)n+1 c)n^2 d)n-1 2- When calling the insert or remove methods, what is an disadvantage for the link-based implementation of the ADT List? a)harder to understand b)must shift data c)searching for that position is slower d)takes more memory 3-What is the last step in the insertion process for a linked implementation of the ADT List? a)Connect the new...
Using C++ Write a template function that accepts an integer parameter and returns its integer square...
Using C++ Write a template function that accepts an integer parameter and returns its integer square root. The function should return -1, if the argument passed is not integer. Demonstrate the function with a suitable driver program .
Please write a class "template" AVLTree in C++. The class must be placed in a single...
Please write a class "template" AVLTree in C++. The class must be placed in a single header file called AVLTree.h The class must contain the following public member functions: -       necessary constructors for your implementation -       destructor -       isEmpty () – determines if the tree is empty - returns 1 if it is empty; 0 otherwise -       height () – returns the height of the node or -1 if a nullptr -       insert () – inserts data into the tree – must abide by AVL...
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings....
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings. You must base your code on the doubly linked list implementation given in my Week 8 slides. Change the code so that instead of an ‘int’ each node stores a string (choose a suitable size). Each node should also have a next node pointer, and previous node pointer. Then write functions to implement the following linked list operations: • A printList function that prints...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT