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 055a51d

Browse files
newProblems
1 parent 9220f1f commit 055a51d

File tree

5 files changed

+206
-2
lines changed

5 files changed

+206
-2
lines changed

‎src/trees/GenericTree.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package trees;
2+
3+
public class GenericTree {
4+
5+
//depth=0;
6+
public static void replaceWithDepthValue(GenericTreeNode<Integer> root,int depth){
7+
8+
if(root==null){
9+
return;
10+
}
11+
12+
root.data=depth;
13+
for(GenericTreeNode<Integer>child:root.children){
14+
15+
replaceWithDepthValue(child,depth+1);
16+
17+
}
18+
}
19+
20+
public static int countLeafNodes(GenericTreeNode<Integer> root){
21+
22+
if(root==null){
23+
return 0;
24+
}
25+
if(root.children.size()==0){
26+
return 1;
27+
}
28+
29+
int count=0;
30+
for(GenericTreeNode<Integer>child:root.children){
31+
count+=countLeafNodes(child);
32+
}
33+
34+
return count;
35+
36+
}
37+
38+
39+
public static GenericTreeNode<Integer> findSecondLargest(GenericTreeNode<Integer> root){
40+
41+
if(root==null){
42+
43+
SecondMax res=new SecondMax();
44+
res.first=null;
45+
res.second=null;
46+
47+
}
48+
49+
SecondMax out=new SecondMax();
50+
out.first=root;
51+
out.second=null;
52+
53+
for(GenericTreeNode<Integer>child:root.children){
54+
55+
56+
57+
}
58+
59+
return null;
60+
61+
}
62+
63+
64+
65+
}
66+
67+
class SecondMax{
68+
69+
GenericTreeNode<Integer> first;
70+
GenericTreeNode<Integer> second;
71+
72+
SecondMax(){
73+
74+
}
75+
76+
77+
}

‎src/trees/GenericTreeNode.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package trees;
2+
3+
import java.util.ArrayList;
4+
5+
public class GenericTreeNode<T> {
6+
T data;
7+
ArrayList<GenericTreeNode<T> > children;
8+
9+
GenericTreeNode(T data){
10+
this.data = data;
11+
children = new ArrayList<GenericTreeNode<T>>();
12+
}
13+
14+
15+
}

‎src/trees/InorderSuccessor.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,80 @@
11
package trees;
22

33
public class InorderSuccessor {
4+
5+
6+
7+
public static BinaryTreeNode<Integer> getMin(BinaryTreeNode<Integer> node)
8+
{
9+
BinaryTreeNode<Integer> current = node;
10+
11+
12+
while (current.left != null) {
13+
current = current.left;
14+
}
15+
return current;
16+
}
17+
18+
19+
public static BinaryTreeNode<Integer> inorderSuccesor(BinaryTreeNode<Integer> root, BinaryTreeNode<Integer> node) {
20+
21+
if(node==null){
22+
return null;
23+
}
24+
else if(node.right!=null){
25+
return getMin(node.right);
26+
}
27+
else{
28+
BinaryTreeNode<Integer> ancestor=root;
29+
BinaryTreeNode<Integer> succ=null;
30+
while(ancestor!=null){
31+
32+
if(node.data<ancestor.data){
33+
succ=ancestor;
34+
ancestor=ancestor.left;
35+
}
36+
else if(node.data>ancestor.data){
37+
38+
ancestor=ancestor.right;
39+
40+
}
41+
else{
42+
43+
break;
44+
}
45+
46+
47+
}
48+
return succ;
49+
}
50+
51+
52+
}
53+
54+
static BinaryTreeNode<Integer> next,res;
55+
public static void reverseInorder(BinaryTreeNode<Integer> root,BinaryTreeNode<Integer>node){
56+
57+
if(root==null) {
58+
return;
59+
}
60+
reverseInorder(root.right,node);
61+
if(root==node){
62+
res=next;
63+
}
64+
65+
next=root;
66+
reverseInorder(root.left,node);
67+
68+
}
69+
70+
71+
72+
public static BinaryTreeNode<Integer> inorderSuccesor2(BinaryTreeNode<Integer> root, BinaryTreeNode<Integer> node) {
73+
74+
res=null;
75+
next=null;
76+
reverseInorder(root,node);
77+
return res;
78+
79+
}
480
}

‎src/trees/KthSmallBST.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,36 @@ public static int kthSmallestNode(BinaryTreeNode<Integer> root,int K){
2828

2929
}
3030

31+
//another way
32+
33+
public static int countallnodes(BinaryTreeNode<Integer> root){
34+
35+
if(root==null){
36+
return 0;
37+
}
38+
return 1+countallnodes(root.left)+countallnodes(root.right);
39+
40+
41+
}
42+
43+
public static int kthSmallestNode2(BinaryTreeNode<Integer> root,int K){
44+
45+
if(root==null){
46+
return Integer.MIN_VALUE;
47+
}
48+
int leftsmallest=countallnodes(root.left);
49+
50+
if(leftsmallest>=K){
51+
return kthSmallestNode2(root.left,K);
52+
}
53+
54+
else if(leftsmallest==K-1){
55+
return root.data;
56+
}
57+
else {
58+
return kthSmallestNode(root.right, K-leftsmallest-1);
59+
}
60+
}
61+
3162

3263
}

‎src/trees/MedianBST.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ public static int count(BinaryTreeNode<Integer>root){
5555
}
5656

5757
public static int median(BinaryTreeNode<Integer> root) {
58+
59+
if(root==null){
60+
return 0;
61+
}
62+
5863
int nodes=count(root);
5964
boolean even=false;
6065
if(nodes%2==0){
@@ -98,7 +103,7 @@ public static int median(BinaryTreeNode<Integer> root) {
98103
else{
99104

100105
pr.right=null;
101-
106+
// prev=pr;
102107
count++;
103108

104109

@@ -121,7 +126,7 @@ public static int median(BinaryTreeNode<Integer> root) {
121126

122127
}
123128

124-
return 0;
129+
return -1;
125130
}
126131

127132
}

0 commit comments

Comments
(0)

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