http://www.ihas1337code.com/2010/11/finding-minimum-window-in-s-which.html
Hi, This blog is the place who want to prepare for programming interview with major product development companies. Apart from interview questions I would like to provide some concepts on the new technologies which I learn as part of my carrier. Thanks for visiting my Blog.
Monday, March 14, 2011
Wednesday, March 2, 2011
Print Ancestors of given Node in a Binary Tree
Question : Print Ancestors of given Node in a Binary Tree ?
Ans :
private static boolean printAncestors(Node root, int target) {
if(root == null)
return false;
else if(root.val == target){
return true;
}else if(printAncestors(root.left,target) || (printAncestors(root.right,target))){
System.out.println("Ancestor : "+root.val);
return true;
}else{
return false;
}
}
Question : Print Level Order traversal of Given BST ?
Ans: private static void printLevelOrderBST(Node node) {
if(queue.isEmpty()){
queue.add(node);
}
while(!queue.isEmpty()) {
Node myNode = queue.remove();
System.out.println(" Value : "+myNode.val);
if(myNode.left != null ){
queue.add(myNode.left);
}
if(myNode.right != null){
queue.add(node.right);
}
}
}
Ans :
private static boolean printAncestors(Node root, int target) {
if(root == null)
return false;
else if(root.val == target){
return true;
}else if(printAncestors(root.left,target) || (printAncestors(root.right,target))){
System.out.println("Ancestor : "+root.val);
return true;
}else{
return false;
}
}
Question : Print Level Order traversal of Given BST ?
Ans: private static void printLevelOrderBST(Node node) {
if(queue.isEmpty()){
queue.add(node);
}
while(!queue.isEmpty()) {
Node myNode = queue.remove();
System.out.println(" Value : "+myNode.val);
if(myNode.left != null ){
queue.add(myNode.left);
}
if(myNode.right != null){
queue.add(node.right);
}
}
}
creating Binary Tree in Java
Question : Write the code to Create the Binary Tree from given elements?
Ans : Example : 1,2,3,4,5,7
Binary Tree: 1
/ \
2 3
/ \
4 5
/
7
Here is the Code :
public class BinaryTreeConstruction {
public static void main(String[] args) {
/* Create root of the Binary Tree */
Node node = new Node(1);
if(root == null){
root = node;
}
int[] values = {2,3,4,5,7};
for(int val: values)
{
constructBinaryTree(node,val);
}
}
/* Construct Binary Tree */
private static void constructBinaryTree(Node node,int val)
Ans : Example : 1,2,3,4,5,7
Binary Tree: 1
/ \
2 3
/ \
4 5
/
7
Here is the Code :
public class BinaryTreeConstruction {
public static void main(String[] args) {
/* Create root of the Binary Tree */
Node node = new Node(1);
if(root == null){
root = node;
}
int[] values = {2,3,4,5,7};
for(int val: values)
{
constructBinaryTree(node,val);
}
}
/* Construct Binary Tree */
private static void constructBinaryTree(Node node,int val)
{
Node newNode = new Node(val);
if(node.left == null)
{
node.left = newNode;
}
else if(node.right == null)
{
node.right = newNode;
}
else if(node.left != null)
{
while(node.left != null && node.right != null)
{
node = node.left;
} if(node.left == null)
{
node.left = newNode;
}else if(node.right == null)
{
node.right = newNode;
}
}
}
}/* Class Close */
Class Node :
public class Node {
int val;
Node left,right;
Node(int value){
left = right = null;
this.val = value;
}
}
Class Node :
public class Node {
int val;
Node left,right;
Node(int value){
left = right = null;
this.val = value;
}
}
Subscribe to:
Posts (Atom)
AWS certification question
AWS AWS Hi! this is for questions related to AWS questions. EC2 instances EC2 storage types cold HDD : 1. Defines performance in terms...
-
Here I am going to discuss some of the differences between Text and String class in Hadoop. Text class lies in the package: import org.ap...
-
Q: What is the library you used in spring to connect to database? Spring JDBC template. Q: What and all involved in fetching the data ...
-
There are 2 ways we can load data into the HIVE Tables. In this tutorial we are loading data into the HIVE managed Table. This is my sa...