Balajiganapathi S
BAN USERAssuming the following structure of the binary tree
struct Node {
Node *left, *right;
};
We can write a recursive function to calculate the maximum depth of a node:
First compute the maximum depth of the two child nodes and take the maximum out of them. This gives us the maximum depth of the children. Now we add 1 to it (for the current node) and this will be the maximum depth starting from the current node. We call the function with the root of the binary tree to get the maximum depth of the whole tree.
int depth(Node *cur) {
if(cur == NULL) return 0;
return 1 + max(depth(cur>left), depth(cur>right));
}

Balajiganapathi S
August 13, 2014 You can use a recursive function which remembers two things: the current index in the input string and the number we have got so far. From here, we can do two things: we can either add the current digit to the number we have so far or we can start a new number.
string s;
int solve(int idx, int sofar) {
if(sofar > 26) return 0;
// There can't be leading zeros
if(sofar == 0) return 0;
// This is a valid splitting of the string
if(idx == (int)s.size()) return 1;
int ret = solve(idx + 1, sofar * 10 + s[idx]  '0');
ret += solve(idx + 1, s[idx]  '0');
return ret;
}
The answer is solve(1, s[0]  '0')
Note that the states may repeat so you can use memoisation to reduce the time complexity to O(n)
This can be solved using dfs.
Consider each person as a node and each connection as an edge between the two persons' corresponding nodes. Then it is easy to see that a community is a connected component in this graph. So use dfs (bfs will work too) to find the number of nodes in each community and if it is even then output that community.
Open Chat in New Window
How is the result computed? Does an element needs to pass all of the filters or any one of them?
 Balajiganapathi S August 14, 2014