Skip to content

Commit e89b8da

Browse files
committed
Java solution 331 && 341
1 parent 962ddbb commit e89b8da

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import java.util.Stack;
2+
3+
/**
4+
* One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #.
5+
* <p>
6+
* _9_
7+
* / \
8+
* 3 2
9+
* / \ / \
10+
* 4 1 # 6
11+
* / \ / \ / \
12+
* # # # # # #
13+
* For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents a null node.
14+
* Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree.
15+
* Each comma separated value in the string must be either an integer or a character '#' representing null pointer.
16+
* You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3".
17+
* <p>
18+
* Example 1:
19+
* "9,3,4,#,#,1,#,#,2,#,6,#,#"
20+
* Return true
21+
* <p>
22+
* Example 2:
23+
* "1,#"
24+
* Return false
25+
* <p>
26+
* Example 3:
27+
* "9,#,#,1"
28+
* Return false
29+
* <p>
30+
* Credits:
31+
* Special thanks to @dietpepsi for adding this problem and creating all test cases.
32+
* <p>
33+
* Created by drfish on 6/9/2017.
34+
*/
35+
public class _331VerifyPreorderSerializationOfABinaryTree {
36+
public boolean isValidSerialization(String preorder) {
37+
Stack<String> stack = new Stack<>();
38+
String[] splits = preorder.split(",");
39+
for (String s : splits) {
40+
while (s.equals("#") && !stack.isEmpty() && stack.peek().equals("#")) {
41+
stack.pop();
42+
if (stack.isEmpty()) {
43+
return false;
44+
}
45+
stack.pop();
46+
}
47+
stack.push(s);
48+
}
49+
return stack.size() == 1 && stack.peek().equals("#");
50+
}
51+
52+
public static void main(String[] args) {
53+
_331VerifyPreorderSerializationOfABinaryTree solution = new _331VerifyPreorderSerializationOfABinaryTree();
54+
assert solution.isValidSerialization("9,3,4,#,#,1,#,#,2,#,6,#,#");
55+
assert !solution.isValidSerialization("1,#");
56+
assert !solution.isValidSerialization("9,#,#,1");
57+
}
58+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import java.util.Iterator;
2+
import java.util.List;
3+
import java.util.Stack;
4+
5+
/**
6+
* Given a nested list of integers, implement an iterator to flatten it.
7+
* Each element is either an integer, or a list -- whose elements may also be integers or other lists.
8+
* <p>
9+
* Example 1:
10+
* Given the list [[1,1],2,[1,1]],
11+
* By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].
12+
* <p>
13+
* Example 2:
14+
* Given the list [1,[4,[6]]],
15+
* By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].
16+
* <p>
17+
* Created by drfish on 6/9/2017.
18+
*/
19+
public class _341FlattenNestedListIterator {
20+
/**
21+
* // This is the interface that allows for creating nested lists.
22+
* // You should not implement it, or speculate about its implementation
23+
**/
24+
public interface NestedInteger {
25+
26+
// @return true if this NestedInteger holds a single integer, rather than a nested list.
27+
public boolean isInteger();
28+
29+
// @return the single integer that this NestedInteger holds, if it holds a single integer
30+
// Return null if this NestedInteger holds a nested list
31+
public Integer getInteger();
32+
33+
// @return the nested list that this NestedInteger holds, if it holds a nested list
34+
// Return null if this NestedInteger holds a single integer
35+
public List<NestedInteger> getList();
36+
}
37+
38+
public class NestedIterator implements Iterator<Integer> {
39+
Stack<NestedInteger> stack = new Stack<>();
40+
41+
public NestedIterator(List<NestedInteger> nestedList) {
42+
for (int i = nestedList.size() - 1; i >= 0; i--) {
43+
stack.push(nestedList.get(i));
44+
}
45+
}
46+
47+
@Override
48+
public Integer next() {
49+
return stack.pop().getInteger();
50+
}
51+
52+
@Override
53+
public boolean hasNext() {
54+
while (!stack.isEmpty()) {
55+
NestedInteger curr = stack.peek();
56+
if (curr.isInteger()) {
57+
return true;
58+
}
59+
stack.pop();
60+
for (int i = curr.getList().size() - 1; i >= 0; i--) {
61+
stack.push(curr.getList().get(i));
62+
}
63+
}
64+
return false;
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)