Skip to content

Commit 5d3bf49

Browse files
committed
Merge pull request haoel#71 from lei-cao/master
Java version
2 parents 248428c + d9e4efd commit 5d3bf49

File tree

70 files changed

+3242
-22
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+3242
-22
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
algorithms-java/out

README.md

+29-22
Large diffs are not rendered by default.

algorithms-java/algorithms-java.iml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="module-library">
11+
<library>
12+
<CLASSES>
13+
<root url="jar://$MODULE_DIR$/junit-4.7.jar!/" />
14+
</CLASSES>
15+
<JAVADOC />
16+
<SOURCES />
17+
</library>
18+
</orderEntry>
19+
</component>
20+
</module>

algorithms-java/junit-4.7.jar

227 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package balancedBinaryTree;
2+
3+
/**
4+
* Created by leicao on 5/10/15.
5+
*/
6+
public class TreeNode {
7+
public int val;
8+
public TreeNode left, right;
9+
public TreeNode(int val) {
10+
this.val = val;
11+
this.left = this.right = null;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Source : https://oj.leetcode.com/problems/balanced-binary-tree/
2+
// Inspired by : http://www.jiuzhang.com/solutions/balanced-binary-tree/
3+
// Author : Lei Cao
4+
// Date : 2015-10-07
5+
6+
/**********************************************************************************
7+
*
8+
* Given a binary tree, determine if it is height-balanced.
9+
*
10+
* For this problem, a height-balanced binary tree is defined as a binary tree in which
11+
* the depth of the two subtrees of every node never differ by more than 1.
12+
* Example
13+
* Given binary tree A={3,9,20,#,#,15,7}, B={3,#,20,15,7}
14+
* The binary tree A is a height-balanced binary tree, but B is not.
15+
**********************************************************************************/
16+
17+
package balancedBinaryTree;
18+
19+
/**
20+
* Created by leicao on 7/10/15.
21+
*/
22+
public class balancedBinaryTree {
23+
/**
24+
* @param root: The root of binary tree.
25+
* @return: True if this Binary tree is Balanced, or false.
26+
*/
27+
public boolean isBalanced(TreeNode root) {
28+
// write your code here
29+
return helper(root, 0).isBalanced;
30+
}
31+
32+
// This is not needed. Can just check the depth
33+
private class Result {
34+
boolean isBalanced;
35+
int height;
36+
Result(boolean isBalanced, int height) {
37+
this.isBalanced = isBalanced;
38+
this.height = height;
39+
}
40+
}
41+
private Result helper(TreeNode root, int depth) {
42+
if (root == null) {
43+
return new Result(true, depth);
44+
}
45+
Result left = helper(root.left, depth + 1);
46+
Result right = helper(root.right, depth + 1);
47+
48+
if (!left.isBalanced || !right.isBalanced) {
49+
return new Result(false, 0);
50+
}
51+
52+
if (Math.abs(left.height - right.height) > 1) {
53+
return new Result(false, 0);
54+
}
55+
return new Result(true, Math.max(left.height, right.height));
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package balancedBinaryTree;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayList;
6+
7+
import static org.junit.Assert.*;
8+
/**
9+
* Created by leicao on 7/10/15.
10+
*/
11+
public class balancedBinaryTreeTest {
12+
13+
@Test
14+
public void testIsBalanced() throws Exception {
15+
ArrayList<TreeNode> inputes = new ArrayList<TreeNode>();
16+
boolean[] results = {false, false};
17+
TreeNode n0 = new TreeNode(0);
18+
TreeNode n1 = new TreeNode(1);
19+
TreeNode n2 = new TreeNode(2);
20+
TreeNode n3 = new TreeNode(3);
21+
22+
n0.left = n1;
23+
n1.left = n2;
24+
n2.left = n3;
25+
26+
TreeNode nn0 = new TreeNode(0);
27+
TreeNode nn1 = new TreeNode(1);
28+
TreeNode nn2 = new TreeNode(2);
29+
TreeNode nn3 = new TreeNode(3);
30+
nn0.right = nn1;
31+
nn1.left = nn2;
32+
nn2.left = nn3;
33+
34+
inputes.add(n0);
35+
inputes.add(nn0);
36+
37+
for (int i = 0; i < results.length; i++) {
38+
balancedBinaryTree b = new balancedBinaryTree();
39+
assertEquals(results[i], b.isBalanced(inputes.get(i)));
40+
}
41+
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package binarySearchTreeIterator;
2+
3+
/**
4+
* Created by leicao on 5/10/15.
5+
*/
6+
public class TreeNode {
7+
public int val;
8+
public TreeNode left, right;
9+
public TreeNode(int val) {
10+
this.val = val;
11+
this.left = this.right = null;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Source : https://oj.leetcode.com/problems/balanced-binary-tree/
2+
// Inspired by : http://www.jiuzhang.com/solutions/binary-search-tree-iterator/
3+
// Author : Lei Cao
4+
// Date : 2015-10-07
5+
6+
/**********************************************************************************
7+
*
8+
* Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.
9+
*
10+
* Calling next() will return the next smallest number in the BST.
11+
*
12+
* Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree.
13+
*
14+
* Credits:Special thanks to @ts for adding this problem and creating all test cases.
15+
*
16+
**********************************************************************************/
17+
18+
package binarySearchTreeIterator;
19+
20+
import java.util.Stack;
21+
22+
/**
23+
* Definition of TreeNode:
24+
* public class TreeNode {
25+
* public int val;
26+
* public TreeNode left, right;
27+
* public TreeNode(int val) {
28+
* this.val = val;
29+
* this.left = this.right = null;
30+
* }
31+
* }
32+
* Example of iterate a tree:
33+
* Solution iterator = new Solution(root);
34+
* while (iterator.hasNext()) {
35+
* TreeNode node = iterator.next();
36+
* do something for node
37+
* }
38+
*/
39+
public class binarySearchTreeIterator {
40+
private TreeNode currentNode = null;
41+
private Stack<TreeNode> stack = new Stack<TreeNode>();
42+
//@param root: The root of binary tree.
43+
public binarySearchTreeIterator(TreeNode root) {
44+
if (root != null) {
45+
currentNode = root;
46+
}
47+
}
48+
49+
//@return: True if there has next node, or false
50+
public boolean hasNext() {
51+
// write your code here
52+
return currentNode != null || !stack.isEmpty();
53+
}
54+
55+
//@return: return next node
56+
public TreeNode next() {
57+
// write your code here
58+
while (currentNode != null) {
59+
stack.push(currentNode);
60+
currentNode = currentNode.left;
61+
}
62+
63+
currentNode = stack.pop();
64+
TreeNode node = currentNode;
65+
currentNode = currentNode.right;
66+
67+
return node;
68+
}
69+
}
70+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package binarySearchTreeIterator;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayList;
6+
7+
import static org.junit.Assert.*;
8+
9+
/**
10+
* Created by leicao on 11/10/15.
11+
*/
12+
public class binarySearchTreeIteratorTest {
13+
@Test
14+
public void testIterator() throws Exception {
15+
ArrayList<TreeNode> inputs = new ArrayList<TreeNode>();
16+
TreeNode t0 = new TreeNode(10);
17+
TreeNode t1 = new TreeNode(1);
18+
TreeNode t2 = new TreeNode(11);
19+
TreeNode t3 = new TreeNode(6);
20+
TreeNode t4 = new TreeNode(12);
21+
t0.left = t1;
22+
t0.right = t2;
23+
t1.right = t3;
24+
t2.right = t4;
25+
26+
inputs.add(t0);
27+
28+
int[][] results = {
29+
{1,6,10,11,12}
30+
};
31+
32+
for (int i = 0; i < results.length; i++) {
33+
binarySearchTreeIterator it = new binarySearchTreeIterator(inputs.get(i));
34+
int j = 0;
35+
while (it.hasNext()) {
36+
TreeNode r = it.next();
37+
assertEquals(results[i][j], r.val);
38+
j++;
39+
}
40+
}
41+
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package binaryTreeBFSTraversal;
2+
3+
/**
4+
* Created by leicao on 5/10/15.
5+
*/
6+
public class TreeNode {
7+
int val;
8+
TreeNode left;
9+
TreeNode right;
10+
TreeNode(int x) { val = x; }
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package binaryTreeBFSTraversal;
2+
3+
import java.util.ArrayList;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.Queue;
7+
8+
/**
9+
* Created by leicao on 5/10/15.
10+
*/
11+
public class binaryTreeBFSTraversal {
12+
public List<Integer> bfsTraversal(TreeNode root) {
13+
List<Integer> results = new ArrayList<Integer>();
14+
if (root == null) {
15+
return results;
16+
}
17+
18+
Queue<TreeNode> q = new LinkedList<TreeNode>();
19+
20+
q.offer(root);
21+
while (q.size() != 0) {
22+
TreeNode n = q.remove();
23+
results.add(n.val);
24+
if (n.left != null) {
25+
q.offer(n.left);
26+
}
27+
if (n.right != null) {
28+
q.offer(n.right);
29+
}
30+
}
31+
32+
return results;
33+
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package binaryTreeBFSTraversal;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
/**
11+
* Created by leicao on 6/10/15.
12+
*/
13+
public class binaryTreeBFSTraversalTest {
14+
15+
/**
16+
* 0
17+
* / \
18+
* 1 2
19+
* / \ \
20+
* 3 4 6
21+
* /
22+
* 5
23+
*/
24+
@Test
25+
public void testPreorderTraversal() throws Exception {
26+
TreeNode t0 = new TreeNode(0);
27+
28+
TreeNode n0 = new TreeNode(0);
29+
TreeNode n1 = new TreeNode(1);
30+
TreeNode n2 = new TreeNode(2);
31+
TreeNode n3 = new TreeNode(3);
32+
TreeNode n4 = new TreeNode(4);
33+
TreeNode n5 = new TreeNode(5);
34+
TreeNode n6 = new TreeNode(6);
35+
36+
n0.left = n1;
37+
n0.right = n2;
38+
n1.left = n3;
39+
n1.right = n4;
40+
n4.left = n5;
41+
n2.right = n6;
42+
43+
44+
ArrayList<TreeNode> inputes = new ArrayList<TreeNode>();
45+
inputes.add(t0);
46+
inputes.add(n0);
47+
48+
List<List<Integer>> results = new ArrayList<List<Integer>>(){{
49+
add(new ArrayList<Integer>(){{
50+
add(0);
51+
}});
52+
add(new ArrayList<Integer>(){{
53+
add(0);
54+
add(1);
55+
add(2);
56+
add(3);
57+
add(4);
58+
add(6);
59+
add(5);
60+
}});
61+
}};
62+
63+
binaryTreeBFSTraversal b = new binaryTreeBFSTraversal();
64+
for (int i = 0; i < results.size(); i++) {
65+
List<Integer> r = b.bfsTraversal(inputes.get(i));
66+
assertEquals(results.get(i), r);
67+
}
68+
69+
}
70+
}

0 commit comments

Comments
 (0)