It is easy to traverse a binary tree in-order using recursion. Do it without using recursion.
Tag Archives: mid-level
Given values of two nodes in a Binary Search Tree, write a c program to find the Lowest Common Ancestor (LCA). You may assume that both the values exist in the tree. You can assume that the nodes are present in the tree.
[The problem appeared in TopCoder SRM 592 (Div-2, Level-1)]
Little Elephant from the Zoo of Lviv has a bunch of books. You are given a int pages. Each element of pages is the number of pages in one of those books. There are no two books with the same number of pages.
You are also given a int number. As a homework from the teacher, Little Elephant must read exactly number of his books during the summer. (Little Elephant has strictly more than number books.)
All other elephants in the school also got the exact same homework. Little Elephant knows that the other elephants are lazy: they will simply pick the shortest number books, so that they have to read the smallest possible total number of pages. Little Elephant wants to be a good student and read a bit more than the other elephants. He wants to pick the subset of books with the second smallest number of pages. In other words, he wants to pick a subset of books with the following properties:
1. There are exactly number books in the chosen subset.
2. The total number of pages of those books is greater than the smallest possible total number of pages.
3. The total number of pages of those books is as small as possible (given the above conditions).
Return the total number of pages Little Elephant will have to read.
Problem link: http://www.codechef.com/AUG13/problems/LELEMON
Little Elephant likes lemonade.
When Little Elephant visits any room, he finds the bottle of the lemonade in that room that contains the greatest number of litres of lemonade and drinks it all.
There are n rooms (numbered from 0 to n-1), each contains bottles. Each bottle has a volume (in litres). The first room visited by Little Elephant was th, the second th, …, the m-th th room. Note that Little Elephant may visit a room more than once.
Find for Little Elephant the total volume of lemonade he has drunk.
Problem link: https://www.hackerrank.com/challenges/detect-html-tags
In this problem you will use regular expressions to help you detect the various Tags used in an HTML document.
Here are a few examples of tags:
The “p” tag for paragraphs:
<p>This is a paragraph</p>
It is also okay to have one or more spaces before or after the tag name:
< p >This is also a paragraph</p>
Then, there is also something called a void or an empty tag such as:
Some tags can also have attributes. For example, here we see the “a” tag which is used for adding links to a document.
There are also tags such as this which haven’t been split into an opening and closing tag:
In the above case, “a” is the tag and “href” is an attribute, the value of which is “http://www.google.com”. Ignore any attributes. Your task is to find out all the tags present in the given document.