Skip to content

Commit ffaa9ec

Browse files
Accepted
1 parent 93d19bb commit ffaa9ec

File tree

3 files changed

+193
-0
lines changed

3 files changed

+193
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import java.util.*;
2+
3+
/**
4+
* Created by gouthamvidyapradhan on 27/03/2017.
5+
* Accepted
6+
*/
7+
public class BoundaryOfBinaryTree
8+
{
9+
public static class TreeNode {
10+
int val;
11+
TreeNode left;
12+
TreeNode right;
13+
TreeNode(int x) { val = x; }
14+
}
15+
16+
private Set<TreeNode> done = new HashSet<>();
17+
/**
18+
* Main method
19+
* @param args
20+
* @throws Exception
21+
*/
22+
public static void main(String[] args) throws Exception
23+
{
24+
TreeNode root = new TreeNode(1);
25+
/*root.right = new TreeNode(2);
26+
root.right.right = new TreeNode(3);
27+
root.right.right.right = new TreeNode(4);
28+
root.right.right.right.left = new TreeNode(5);
29+
root.right.right.right.left.right = new TreeNode(6);
30+
/*root.left = new TreeNode(2);
31+
root.left.left = new TreeNode(4);
32+
root.left.right = new TreeNode(5);
33+
root.left.right.left = new TreeNode(7);
34+
root.left.right.right = new TreeNode(8);
35+
root.right = new TreeNode(3);
36+
root.right.left = new TreeNode(6);
37+
root.right.left.left = new TreeNode(9);
38+
root.right.left.right = new TreeNode(10);*/
39+
System.out.println(new BoundaryOfBinaryTree().boundaryOfBinaryTree(root));
40+
}
41+
42+
public List<Integer> boundaryOfBinaryTree(TreeNode root)
43+
{
44+
if(root == null) return new ArrayList<>();
45+
List<Integer> antiClockwiseCollection = new ArrayList<>();
46+
List<Integer> collection = new ArrayList<>();
47+
48+
if(root.left != null)
49+
leftShoulder(root, collection);
50+
else
51+
{
52+
if(!done.contains(root))
53+
{
54+
done.add(root);
55+
collection.add(root.val);
56+
}
57+
}
58+
59+
antiClockwiseCollection.addAll(collection);
60+
collection.clear();
61+
leafNode(root, collection);
62+
antiClockwiseCollection.addAll(collection);
63+
collection.clear();
64+
65+
if(root.right != null)
66+
rightShoulder(root, collection);
67+
else
68+
{
69+
if(!done.contains(root))
70+
{
71+
done.add(root);
72+
collection.add(root.val);
73+
}
74+
}
75+
76+
Stack<Integer> stack = new Stack<>();
77+
stack.addAll(collection);
78+
while(!stack.isEmpty())
79+
antiClockwiseCollection.add(stack.pop());
80+
return new ArrayList<>(antiClockwiseCollection);
81+
}
82+
83+
private void leftShoulder(TreeNode node, List<Integer> list)
84+
{
85+
if(node == null) return;
86+
if(!done.contains(node))
87+
{
88+
list.add(node.val);
89+
done.add(node);
90+
}
91+
92+
if(node.left != null) leftShoulder(node.left, list);
93+
else if(node.right != null) leftShoulder(node.right, list);
94+
}
95+
96+
private void rightShoulder(TreeNode node, List<Integer> list)
97+
{
98+
if(node == null) return;
99+
if(!done.contains(node))
100+
{
101+
list.add(node.val);
102+
done.add(node);
103+
}
104+
if(node.right != null) rightShoulder(node.right, list);
105+
else if(node.left != null) rightShoulder(node.left, list);
106+
}
107+
108+
private void leafNode(TreeNode node, List<Integer> list)
109+
{
110+
if(node == null) return;
111+
if(node.left == null && node.right == null)
112+
if(!done.contains(node))
113+
{
114+
list.add(node.val);
115+
done.add(node);
116+
}
117+
leafNode(node.left, list);
118+
leafNode(node.right, list);
119+
}
120+
}

problems/src/KdiffPairsInanArray.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.util.*;
2+
3+
/**
4+
* Created by gouthamvidyapradhan on 28/03/2017.
5+
*/
6+
public class KdiffPairsInanArray
7+
{
8+
private Map<Integer, Integer> map = new HashMap<>();
9+
private int count = 0;
10+
/**
11+
* Main method
12+
* @param args
13+
* @throws Exception
14+
*/
15+
public static void main(String[] args) throws Exception
16+
{
17+
int[] nums = {1,2,3,4,5};
18+
System.out.println(new KdiffPairsInanArray().findPairs(nums, -1));
19+
}
20+
21+
public int findPairs(int[] nums, int k)
22+
{
23+
if(nums.length == 0 || k < 0) return 0;
24+
for(int i : nums)
25+
{
26+
map.put(i, map.getOrDefault(i, 0) + 1);
27+
}
28+
for(Map.Entry<Integer, Integer> entry : map.entrySet())
29+
{
30+
if(k == 0)
31+
{
32+
if(entry.getValue() > 1)
33+
count ++;
34+
}
35+
else
36+
{
37+
if(map.containsKey(entry.getKey() + k))
38+
count ++;
39+
}
40+
}
41+
return count;
42+
}
43+
44+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Created by gouthamvidyapradhan on 27/03/2017.
3+
*/
4+
public class LongestPalindromicSubsequence
5+
{
6+
/**
7+
* Main method
8+
* @param args
9+
* @throws Exception
10+
*/
11+
public static void main(String[] args) throws Exception
12+
{
13+
System.out.println(new LongestPalindromicSubsequence().longestPalindromeSubseq("bbbab"));
14+
}
15+
16+
public int longestPalindromeSubseq(String s)
17+
{
18+
int[][] dp = new int[s.length() + 1][s.length() + 1];
19+
String sI = new StringBuilder(s).reverse().toString();
20+
for(int i = 1, l = s.length(); i <= l; i++)
21+
for(int j = 1; j <= l; j++)
22+
{
23+
dp[i][j] = (s.charAt(i - 1) == sI.charAt(j - 1)) ? dp[i - 1][j - 1] + 1 : Math.max(dp[i - 1][j], dp[i][j - 1]);
24+
}
25+
26+
return dp[s.length()][s.length()];
27+
}
28+
29+
}

0 commit comments

Comments
 (0)