Binary Tree Zigzag Level Order Traversal

Given a binary Tree, write a code to return binary tree zigzag level order traversal of its node’s values. (ie, from left to right, then right to left for the next level and alternate between).

In the screenshot below, We have printed the zigzag traversal of a binary tree. We have printed the values from left to right for the first level. For the second level, we then moved from right to left. Then for the next level, we move from left to right and so on.

Reverse Words in a String

How to reverse words in a String?

In this problem, we have given an input string, we have to write a code to reverse the string word by word.

Example 1:

Input : “the carpet is green”

Output: “green is carpet the”

Example 2:

Input : ”  Java Ebook  “

Output: “Ebook Java”

Explanation: The reversed string should not contain leading or trailing spaces.

Example 3:

Input : “a good   example”

Output: “example good a”

Explanation: We have reduce multiple spaces between two words to a single space in the reversed string.

Note:

  • The Input string may contain leading or trailing spaces. However, the reversed string should not contain leading or trailing spaces.
  • We have to reduce multiple spaces between two words to a single space in the reversed string.

Sort a Stack using Recursion

In this problem, We have to write a code to sort a stack using recursion. We have to sort in a descending order (Top of the stack has the greatest element).

We don’t have to use any loop constructs ( for, while etc) or additional data structure.

For Example –

In this example, You can see after sorting the stack, the element which has greater value is at the top of the stack.

Sort a stack using recursion