Skip to content

Commit a3d34fc

Browse files
committed
binary tree
nodes at k distance from current node pre order successor
1 parent a93eedd commit a3d34fc

19 files changed

+88
-30
lines changed

coding-todo

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package Array;
2+
3+
/**
4+
* Created by dheeraj on 7/11/2016.
5+
*/
6+
public class ReplaceZeroOneToFindLongest1 {
7+
public static void main(String[] strings) {
8+
int[] array = new int[]{1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1};
9+
10+
int start, rep, length;
11+
12+
for (int x = 0; x < array.length; x++) {
13+
14+
}
15+
16+
}
17+
}

src/BinaryTree/KDistanceFromANode.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
package BinaryTree;
22

3+
4+
import com.sun.deploy.model.Resource;
5+
6+
import java.io.File;
7+
import java.security.CodeSigner;
8+
import java.security.cert.Certificate;
9+
import java.util.Map;
10+
import java.util.jar.JarFile;
11+
312
/**
413
* Created by dheeraj on 7/10/2016.
514
*/
@@ -17,34 +26,33 @@ private static void printAtKFromCurrent(Node root, int currentDis, int diatance)
1726
printAtKFromCurrent(root.right, currentDis + 1, diatance);
1827
}
1928

20-
private static int findDis(Node node, int value,int distance) {
29+
private static int findDis(Node node, int value, int distance) {
2130
if (node == null) {
2231
return -1;
2332
}
2433
if (node.data == value) {
25-
printAtKFromCurrent(node,0,distance);
34+
printAtKFromCurrent(node, 0, distance);
2635
return 1;
2736
}
28-
int left = findDis(node.left, value,distance);
29-
int right = findDis(node.right, value,distance);
37+
int left = findDis(node.left, value, distance);
38+
int right = findDis(node.right, value, distance);
3039

3140
if (left > -1) {
32-
if(left == distance){
41+
if (left == distance) {
3342
System.out.println(node.data);
3443
}
35-
printAtKFromCurrent(node.right,1,distance-left);
36-
return left+1;
44+
printAtKFromCurrent(node.right, 1, distance - left);
45+
return left + 1;
3746
} else if (right > -1) {
38-
if(right == distance){
47+
if (right == distance) {
3948
System.out.println(node.data);
4049
}
41-
printAtKFromCurrent(node.left,1,distance-right);
42-
return right+1;
50+
printAtKFromCurrent(node.left, 1, distance - right);
51+
return right + 1;
4352
}
4453
return -1;
4554
}
4655

47-
4856
public static void main(String[] strings) {
4957
Node node = new Node(1);
5058

@@ -60,6 +68,6 @@ public static void main(String[] strings) {
6068
node.left.left.left = new Node(8);
6169
node.left.left.left.left = new Node(9);
6270
node.left.left.right = new Node(10);
63-
findDis(node,4,2);
71+
findDis(node, 4, 2);
6472
}
6573
}

src/BinaryTree/TreeFromInOrderLevelOrder.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package BinaryTree;
22

3-
import javax.management.RuntimeMBeanException;
4-
53
/**
64
* Created by dheeraj on 7/9/2016.
75
*/

src/DP/AllPermutationsOfString.java renamed to src/dymanicProgramming/AllPermutationsOfString.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package DP;
1+
package dymanicProgramming;
22

33
import java.util.*;
44

src/DP/AppleCollectorTopCoder.java renamed to src/dymanicProgramming/AppleCollectorTopCoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package DP;
1+
package dymanicProgramming;
22

33
/**
44
* Created by dheeraj on 12/25/14.

src/DP/Beginner_CoinsSum.java renamed to src/dymanicProgramming/Beginner_CoinsSum.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package DP;
1+
package dymanicProgramming;
22

33
import java.util.Hashtable;
44

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package DP;
1+
package dymanicProgramming;
22

33
/**
44
* Created by dheeraj on 6/7/16.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package DP;
1+
package dymanicProgramming;
22

33
import java.util.Arrays;
44

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package dymanicProgramming;
2+
3+
/**
4+
* Created by dheeraj on 7/10/2016.
5+
*/
6+
public class KnapsackProblem {
7+
8+
//10 -> 60
9+
//20 -> 100
10+
//30 -> 120
11+
12+
static int knapSack(int W, int wt[], int val[], int n) {
13+
// Base Case
14+
if (n == 0 || W == 0)
15+
return 0;
16+
17+
// If weight of the nth item is more than Knapsack capacity W, then
18+
// this item cannot be included in the optimal solution
19+
if (wt[n] > W)
20+
return knapSack(W, wt, val, n - 1);
21+
22+
// Return the maximum of two cases:
23+
// (1) nth item included
24+
// (2) not included
25+
else return Math.max(val[n] + knapSack(W - wt[n], wt, val, n-1),
26+
knapSack(W, wt, val, n-1)
27+
);
28+
}
29+
30+
public static void main(String[] strings) {
31+
int val[] = {60, 100, 120};
32+
int wt[] = {10, 20, 30};
33+
int W = 50;
34+
int n = val.length-1;
35+
System.out.println(knapSack(W, wt, val, n));
36+
}
37+
}

0 commit comments

Comments
 (0)