Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9220f1f

Browse files
newProblems
1 parent 9a8fe59 commit 9220f1f

File tree

9 files changed

+537
-62
lines changed

9 files changed

+537
-62
lines changed

‎src/recursion/ways.jav

Lines changed: 0 additions & 62 deletions
This file was deleted.

‎src/recursion/ways.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package recursion;
2+
3+
public class ways {
4+
public static int helper(int input[], int target, int csum, int i) {
5+
6+
7+
if (i == input.length) {
8+
if (csum == target)
9+
return 1;
10+
else
11+
return 0;
12+
}
13+
14+
15+
//recursive calls
16+
17+
//include element +
18+
int add = helper(input, target, csum + input[i], i + 1);
19+
20+
21+
//include element -
22+
int sub = helper(input, target, csum - input[i], i + 1);
23+
24+
// not included
25+
26+
int exc = helper(input, target, csum, i + 1);
27+
28+
29+
return add + sub + exc;
30+
31+
32+
}
33+
34+
public static int numberOfWays(int[] input, int sum) {
35+
//Your Code goes here
36+
37+
38+
39+
int ans= helper(input,sum,0,0);
40+
if(sum==0){
41+
return ans-1;
42+
43+
}
44+
45+
else{
46+
return ans;
47+
}
48+
}
49+
50+
}
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
61+

‎src/trees/InorderSuccessor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package trees;
2+
3+
public class InorderSuccessor {
4+
}

‎src/trees/KthSmallBST.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package trees;
2+
3+
public class KthSmallBST {
4+
5+
static int pos=0;
6+
public static int kthSmallestNode(BinaryTreeNode<Integer> root,int K){
7+
8+
if(root==null){
9+
10+
return Integer.MIN_VALUE;
11+
}
12+
13+
int leftsmallest=kthSmallestNode(root.left,K);
14+
15+
if(leftsmallest!=Integer.MIN_VALUE){
16+
17+
return leftsmallest;
18+
}
19+
20+
pos++;
21+
22+
if(pos==K){
23+
24+
return root.data;
25+
}
26+
27+
return kthSmallestNode(root.right,K);
28+
29+
}
30+
31+
32+
}

‎src/trees/MedianBST.java

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package trees;
2+
3+
public class MedianBST {
4+
5+
public static BinaryTreeNode<Integer> inorderpred(BinaryTreeNode<Integer>root){
6+
7+
BinaryTreeNode<Integer>temp=root.left;
8+
9+
while(temp.right!=root && temp.right!=null){
10+
11+
temp=temp.right;
12+
}
13+
14+
return temp;
15+
}
16+
17+
public static int count(BinaryTreeNode<Integer>root){
18+
19+
BinaryTreeNode<Integer> curr=root;
20+
21+
int count=0;
22+
while(curr!=null){
23+
24+
if(curr.left==null){
25+
26+
count++;
27+
curr=curr.right;
28+
29+
}
30+
31+
else{
32+
33+
BinaryTreeNode<Integer>pr=inorderpred(curr);
34+
35+
if(pr.right==null){
36+
37+
pr.right=curr;
38+
curr=curr.left;
39+
40+
}
41+
else{
42+
43+
pr.right=null;
44+
45+
count++;
46+
curr=curr.right;
47+
48+
}
49+
50+
}
51+
52+
}
53+
return count;
54+
55+
}
56+
57+
public static int median(BinaryTreeNode<Integer> root) {
58+
int nodes=count(root);
59+
boolean even=false;
60+
if(nodes%2==0){
61+
even=true;
62+
}
63+
BinaryTreeNode<Integer> curr=root;
64+
BinaryTreeNode<Integer>prev=null;
65+
int count=0;
66+
while(curr!=null){
67+
68+
if(curr.left==null){
69+
70+
count++;
71+
72+
if(!even && count==(nodes+1)/2){
73+
74+
return curr.data;
75+
}
76+
77+
if(even && count==nodes/2+1){
78+
79+
return (prev.data+curr.data)/2;
80+
}
81+
82+
prev=curr;
83+
curr=curr.right;
84+
85+
}
86+
87+
else{
88+
89+
BinaryTreeNode<Integer>pr=inorderpred(curr);
90+
91+
if(pr.right==null){
92+
93+
pr.right=curr;
94+
curr=curr.left;
95+
96+
}
97+
98+
else{
99+
100+
pr.right=null;
101+
102+
count++;
103+
104+
105+
if(!even && count==(nodes+1)/2){
106+
107+
return curr.data;
108+
}
109+
110+
if(even && count==nodes/2+1){
111+
112+
return (prev.data+curr.data)/2;
113+
}
114+
115+
prev=curr;
116+
curr=curr.right;
117+
118+
}
119+
120+
}
121+
122+
}
123+
124+
return 0;
125+
}
126+
127+
}

‎src/trees/ReplaceWithLeastGreater.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,37 @@ public NodeX(int val){
2424

2525
}
2626

27+
// another approach to get successor...
28+
//public static NodeX getSucc(NodeX root,int curr){
29+
//
30+
// NodeX node=getNode(root,curr);
31+
// if(node==null){
32+
// return null;
33+
// }
34+
// else if(node.right!=null){
35+
// return getMin(node.right);
36+
// }
37+
// else{
38+
// NodeX ancestor=root,NodeX succ=null;
39+
// while(node!=ancestor){
40+
//
41+
// if(node.data<ancestor.data){
42+
// succ=ancestor;
43+
// ancestor=ancestor.left;
44+
// }
45+
// else{
46+
// ancestor=ancestor.right;
47+
// }
48+
//
49+
//
50+
// }
51+
// return succ;
52+
// }
53+
//
54+
//}
55+
56+
57+
2758
public class ReplaceWithLeastGreater {
2859

2960
public static NodeX createbst(NodeX root,int val,succ obj)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /