Aswinvandev
BAN USER/*
* Depth of a tree without recursion
*/
public void depthIter(TreeNode node){
Stack<TreeNode> stack = new Stack<TreeNode>();
int ht=0, max=0;
boolean flag=false;
stack.push(node);
while(!stack.empty()){
flag = false;
node = stack.pop();
if(node.rightChild!=null){
stack.push(node.rightChild);
flag = true;
}
if(node.leftChild!=null){
stack.push(node.rightChild);
flag=true;
}
if(flag){
++ht;
if(ht>max)
max = ht;
}else{
ht--;
}
}
System.out.println("max depth: "+max);
}
public class StringMatch {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] input = new String[5];
String[] replace = new String[5];
input[0] = "hello";
input[1] = "is";
input[2] = "madara";
input[3] = "luffy";
input[4] = "tobi";
replace[0] = "hi";
replace[1] = "was";
replace[2] = "itachi";
replace[3] = "dragon";
replace[4] = "obito";
boolean flag = false;
String text = new String();
text = "hello is madara there? this is tobi here";
Scanner sc = new Scanner(text);
String inp;
StringBuilder out = new StringBuilder();
while(sc.hasNext()){
inp = sc.next();
for(int i=0; i<input.length;i++){
flag = false;
if(inp.compareTo(input[i])==0){
inp = replace[i];
out.append(inp);
out.append(" ");
break;
}else{
flag = true;
continue;
}
}
if(flag){
out.append(inp);
out.append(" ");
}
}
System.out.print(out);
}
}
Time Complexity: O(m+n)
- Aswinvandev November 01, 2014m = maxNum
n = no. of elem