Skip to content

Commit fe70d1e

Browse files
committed
06/30/2019
1 parent 3253728 commit fe70d1e

File tree

4 files changed

+183
-1
lines changed

4 files changed

+183
-1
lines changed

src/DSA/Trees/MorrisTraversal.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,31 @@ static void MorrisTraverseInorder(node root){
3636
}
3737

3838
}
39+
static void MorrisTraversePostOrder(node root){
40+
if(root==null)
41+
return;
42+
while(root!=null) {
43+
if (root.right == null) {
44+
System.out.print(root.key + " ");
45+
root = root.left;
46+
} else {
47+
node right = root.right;
48+
while (right.right != null && right.right != root)
49+
right = right.left;
50+
if (right.left == null) {
51+
right.left = root;
52+
root = root.right;
53+
}else {
54+
right.left = null;
55+
System.out.print(root.key+" ");
56+
root = root.left;
57+
}
58+
59+
}
60+
}
61+
62+
}
63+
3964

4065
static void MorrisTraversePreorder(node root){
4166
if(root==null)
@@ -70,6 +95,6 @@ public static void main(String[] args) {
7095
root.left.left = new node(4);
7196
root.left.right = new node(5);
7297

73-
MorrisTraversePreorder(root);
98+
MorrisTraversePostOrder(root);
7499
}
75100
}

src/DSA/Trees/PathNode.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package DSA.Trees;
2+
3+
import java.util.ArrayList;
4+
import java.util.Stack;
5+
6+
public class PathNode {
7+
static class node {
8+
int key;
9+
node left, right;
10+
11+
public node(int key) {
12+
this.key = key;
13+
left = right = null;
14+
}
15+
}
16+
17+
static boolean hasPath(node root, int data, ArrayList<Integer> arr) {
18+
if (root == null) {
19+
return false;
20+
}
21+
arr.add(root.key);
22+
if (root.key == data) {
23+
return true;
24+
}
25+
if(hasPath(root.left, data, arr) || hasPath(root.right, data, arr))
26+
return true;
27+
28+
arr.remove(arr.size()-1);
29+
return false;
30+
}
31+
32+
public static void main(String[] args) {
33+
node root=new node(1);
34+
root.left = new node(2);
35+
root.right = new node(3);
36+
root.left.left = new node(4);
37+
root.left.right = new node(5);
38+
root.right.left = new node(6);
39+
root.right.right = new node(7);
40+
int x=5;
41+
PrintPath(root, 6);
42+
}
43+
44+
private static void PrintPath(node root, int x) {
45+
ArrayList<Integer> arr = new ArrayList<>();
46+
47+
if(hasPath(root, x,arr)){
48+
49+
for(int i: arr){
50+
System.out.print(i+" ");
51+
}
52+
}
53+
}
54+
}

src/DSA/Trees/PrintOddNodes.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package DSA.Trees;
2+
3+
public class PrintOddNodes {
4+
static class node {
5+
int key;
6+
node left, right;
7+
8+
public node(int key) {
9+
this.key = key;
10+
left = right = null;
11+
}
12+
}
13+
14+
static void printOddnodes(node root) {
15+
int h = height(root);
16+
for (int i = 1; i <= h; i += 2) {
17+
printOddnodesUtil(root, i);
18+
}
19+
}
20+
21+
private static void printOddnodesUtil(node root, int i) {
22+
if (i == 1) {
23+
System.out.print(root.key + " ");
24+
}
25+
if(root.left!=null)
26+
printOddnodesUtil(root.left, i - 1);
27+
if(root.right!=null)
28+
printOddnodesUtil(root.right, i - 1);
29+
30+
}
31+
32+
static int height(node root) {
33+
if (root == null)
34+
return 0;
35+
36+
return 1 + Math.max(height(root.left), height(root.right));
37+
}
38+
39+
public static void main(String[] args) {
40+
node root = new node(1);
41+
root.left = new node(2);
42+
root.right = new node(3);
43+
root.left.left = new node(4);
44+
root.left.right = new node(5);
45+
root.right.right = new node(6);
46+
root.left.right.left = new node(7);
47+
root.left.right.right = new node(8);
48+
root.right.right.left = new node(9);
49+
printOddnodes(root);
50+
51+
52+
}
53+
54+
}

src/DSA/Trees/TreeAndMirror.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package DSA.Trees;
2+
3+
public class TreeAndMirror {
4+
static class node{
5+
int key;
6+
node left, right;
7+
8+
public node(int key) {
9+
this.key = key;
10+
left = right = null;
11+
}
12+
}
13+
14+
static class BinaryTree{
15+
node a, b;
16+
17+
boolean areMirror(node a, node b){
18+
if(a==null&&b==null)
19+
return true;
20+
else if((a==null && b!=null) || (a!=null&&b==null)){
21+
return false;
22+
}
23+
24+
if(a.key!=b.key)
25+
return false;
26+
return areMirror(a.left, b.right) && areMirror(a.right,b.left);
27+
}
28+
}
29+
30+
public static void main(String[] args) {
31+
BinaryTree tree = new BinaryTree();
32+
node a = new node(1);
33+
node b = new node(1);
34+
a.left = new node(2);
35+
a.right = new node(3);
36+
a.left.left = new node(4);
37+
a.left.right = new node(5);
38+
39+
b.left = new node(4);
40+
b.right = new node(2);
41+
b.right.left = new node(5);
42+
b.right.right = new node(4);
43+
44+
if (tree.areMirror(a, b))
45+
System.out.println("Yes");
46+
else
47+
System.out.println("No");
48+
}
49+
}

0 commit comments

Comments
 (0)