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*...
Write a C program to print the elements of an array in reverse order using pointer.
Write a C program to print the elements of an array in reverse order using pointer.
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.
C++ Generate 100 random numbers of the values 1-20 in an input.txt. Now create a binary...
C++ Generate 100 random numbers of the values 1-20 in an input.txt. Now create a binary search tree using the numbers of the sequence. The tree set should not have any nodes with same values and all repeated numbers of the random sequence must be stored in the node as a counter variable. For example, if there are five 20s’ in the random sequence then the tree node having data 20 has counter variable equal to 5. Sort the sequence...
You are given a reference to the root node of a binary search tree, that implements...
You are given a reference to the root node of a binary search tree, that implements a dictionary data structure. Please print all the elements in depths 500 through 510, all in sorted order. A node in a binary search tree is at depth x, if it takes x hops to get from the root. So the root is at depth 0, the children of the root are at depth 1, and so on. The class TreeNode defines a single...
1. Create a linked list using the Node class that contains the first million prime numbers...
1. Create a linked list using the Node class that contains the first million prime numbers (primes4.txt). It should have a menu with these options: 1 = Search for a Number (let the user enter a number to search for) 2 = Add a new Number (let the user enter a number and add it to the head of the list) 3 = Delete a Number (let the user enter a number and delete it if found) 4 = Exit...
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
There are two ways to write loops: (1) iterative, like the for-loops we're used to using,...
There are two ways to write loops: (1) iterative, like the for-loops we're used to using, and (2) recursive. Your prerequisite preparation for this course should have exposed you to both, although your working knowledge of recursive loops may not be as strong as that of iterative loops. Consider the following iterative function that prints an array of characters backward: #include <iostream> #include <cstring> // print an array backwards, where 'first' is the first index // of the array, and...