l33t
BAN USER- -4of 4 votes
AnswersSwap 2 nodes in a Binary tree.(May or Maynot be a BST)
- l33t in United States for ChromeCast
Swap the nodes NOT just their values.
(preferably in Java please..(My requirement not theirs :p))
ex:
5
/ \
3 7
/ \ /
2 4 6
swap 7 and 3
5
/ \
7 3
/ / \
6 2 4| Report Duplicate | Flag | PURGE
Google Software Engineer / Developer Algorithm - 0of 0 votes
AnswersSuppose you have a million integer numbers.
- l33t in United States for Hangouts
Return all possible values of a,b and c such that
a+b+c<=d.
d will be provided to you.
ex: if the numbers are 1,2,3,4,5,6,7
and d=7
[1,2,3]
[1,2,4]
[1,2,3] will be same as [1,3,2] and [3,2,1]...
follow up:
Return all such parts that satisfy the above condition if d is not provided.| Report Duplicate | Flag | PURGE
Google Software Engineer / Developer Algorithm
class TrieNode {
char c;
TrieNode links[];
boolean fullWord;
TrieNode(char c)
{
this.c=c;
links=new TrieNode[26];
fullWord=false;
}
}
now for insert and printing methods
// root is supposed to be made something like this root=new TrieNode('!');
public static void makeTrie(String s,TrieNode root)
{
s=s.toLowerCase();
TrieNode temp=root;
for(int i=0;i<s.length();i++)
{
int t=s.charAt(i)-'a';
while(temp.links[t]==null)
{
temp=temp.links[t];
t=s.charAt(++i)-'a';
}
if(i!=s.length()-1)
{
char c=(char)(t+'a');
temp.links[t]=new TrieNode(c);
}
else
{
char c=(char)(t+'a');
temp.links[t]=new TrieNode(c);
temp.links[t].fullWord=true;
}
temp=temp.links[t];
}
}
public static void readTrie(String s,TrieNode root)
{
if(s.isEmpty() ||s.equals("!"))
return;
TrieNode temp=root;
String match="";
int i=0;
while(i!=s.length())
{
int t=s.charAt(i)-'a';
temp=temp.links[t];
if(temp==null)
break;
match=match+temp.c;
i++;
}
if(match.length()>0)
match=match.substring(0,match.length()-1);
printAll(temp,match);
}
public static void printAll(TrieNode t,String parent)
{
if(t==null)
return;
parent=parent+t.c;
if(t.fullWord)
System.out.println(parent);
for(int i=0;i<26;i++)
printAll(t.links[i],parent);
}
public class NextHigh {
public static int makeNumber(int a[])
{
int ans=0;
for(int i=a.length-1;i>=0;i--)
{
ans=a[i]+ans*10;
}
return ans;
}
public static int func(int b[])
{
int a[]=new int[b.length];
a=Arrays.copyOf(b, b.length);
int n=a.length,ans=makeNumber(b);
int i,t;
boolean found=false;
int f,s;
for(i=0;i<n-1;i++)
{
f=makeNumber(a);
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
s=makeNumber(a);
if(s>f)
{
found=true;
break;
}
else
a=Arrays.copyOf(b, b.length);
}
if(found)
ans=makeNumber(a);
return ans;
}
public static void main(String args[])
{
int n=11;
int t=n;
int len=0;
while(t>0)
{
len++;
t=t/10;
}
int a[]=new int[len];
t=n;
for(int i=0;i<len;i++)
{
a[i]=t%10;
t=t/10;
}
System.out.println(makeNumber(a));
System.out.println(func(a));
}
}
the signature was something like this
- l33t June 22, 2014