Skip to content

Commit af23d6c

Browse files
committed
Add Binary Tree problems
1 parent 87de217 commit af23d6c

10 files changed

+240
-4
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.andrewbayd;
2+
3+
import com.andrewbayd.datastructures.TreeNode;
4+
5+
/*
6+
Given a binary tree, determine if it is height-balanced.
7+
8+
https://leetcode.com/problems/balanced-binary-tree/
9+
*/
10+
11+
public class BalancedBinaryTree {
12+
public boolean isBalanced(TreeNode root) {
13+
return height(root) != -1;
14+
}
15+
16+
private int height(TreeNode node) {
17+
if (node == null) return 0;
18+
19+
int leftHeight = height(node.left);
20+
int rightHeight = height(node.right);
21+
22+
if (leftHeight == -1 || rightHeight == -1 || Math.abs(leftHeight - rightHeight) > 1) return -1;
23+
24+
return Math.max(leftHeight, rightHeight) + 1;
25+
}
26+
}

src/com/andrewbayd/BinaryTreeInOrderTraversal.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package com.andrewbayd.binaryTreeInOrderTraversal;
1+
package com.andrewbayd;
2+
3+
import com.andrewbayd.datastructures.TreeNode;
24

35
import java.util.ArrayList;
46
import java.util.List;

src/com/andrewbayd/BinaryTreeLCA.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.andrewbayd;
2+
3+
import com.andrewbayd.datastructures.TreeNode;
4+
5+
/*
6+
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
7+
8+
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
9+
*/
10+
public class BinaryTreeLCA {
11+
public static TreeNode lca(TreeNode root, TreeNode node1, TreeNode node2) {
12+
if (root == null) return null;
13+
if (root.equals(node1) || root.equals(node2)) return root;
14+
15+
TreeNode leftLCA = lca(root.left, node1, node2);
16+
TreeNode rightLCA = lca(root.right, node1, node2);
17+
18+
if (leftLCA != null && rightLCA != null) return root;
19+
if (leftLCA != null) return leftLCA;
20+
return rightLCA;
21+
}
22+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.andrewbayd;
2+
3+
import com.andrewbayd.datastructures.TreeNode;
4+
5+
import java.util.ArrayDeque;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
/*
10+
Given the root of a binary tree, return the level order traversal of its nodes' values.
11+
12+
https://leetcode.com/problems/binary-tree-level-order-traversal/
13+
*/
14+
public class BinaryTreeLevelOrderTraversal {
15+
public List<List<Integer>> levelOrder(TreeNode root) {
16+
List<List<Integer>> result = new ArrayList<>();
17+
ArrayDeque<TreeNode> q = new ArrayDeque<>();
18+
19+
if (root != null) q.add(root);
20+
while (!q.isEmpty()) {
21+
List<Integer> levelItems = new ArrayList<>();
22+
int levelSize = q.size();
23+
for (int i = 0; i < levelSize; i++) {
24+
TreeNode current = q.poll();
25+
if (current.left != null) q.add(current.left);
26+
if (current.right != null) q.add(current.right);
27+
levelItems.add(current.val);
28+
}
29+
result.add(levelItems);
30+
}
31+
return result;
32+
}
33+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.andrewbayd;
2+
3+
import com.andrewbayd.datastructures.TreeNode;
4+
5+
import java.util.ArrayDeque;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
/*
10+
Given the root of a binary tree, return the zigzag level order traversal of its nodes' values. (i.e., from left to right, then right to left for the next level and alternate between).
11+
12+
https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
13+
*/
14+
public class BinaryTreeZigZagLevelTraversal {
15+
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
16+
List<List<Integer>> result = new ArrayList<>();
17+
ArrayDeque<TreeNode> q = new ArrayDeque<>();
18+
19+
if (root != null) q.add(root);
20+
while (!q.isEmpty()) {
21+
List<Integer> levelItems = new ArrayList<>();
22+
int levelSize = q.size();
23+
boolean isReversed = result.size() % 2 != 0;
24+
for (int i = 0; i < levelSize; i++) {
25+
TreeNode current = isReversed ? q.pollLast() : q.pollFirst();
26+
levelItems.add(current.val);
27+
if (isReversed) {
28+
if (current.right != null) q.addFirst(current.right);
29+
if (current.left != null) q.addFirst(current.left);
30+
} else {
31+
if (current.left != null) q.addLast(current.left);
32+
if (current.right != null) q.addLast(current.right);
33+
}
34+
}
35+
result.add(levelItems);
36+
}
37+
return result;
38+
}
39+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.andrewbayd;
2+
3+
import com.andrewbayd.datastructures.TreeNode;
4+
5+
/*
6+
Given a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X.
7+
Return the number of good nodes in the binary tree.
8+
9+
https://leetcode.com/problems/count-good-nodes-in-binary-tree/
10+
*/
11+
public class GoodNodesInBinaryTree {
12+
public int goodNodes(TreeNode root) {
13+
return dfs(root, root.val);
14+
}
15+
16+
private int dfs(TreeNode root, int max) {
17+
if (root == null) return 0;
18+
int count = 0;
19+
if (root.val >= max) {
20+
count++;
21+
max = root.val;
22+
}
23+
count += dfs(root.left, max);
24+
count += dfs(root.right, max);
25+
return count;
26+
}
27+
}

src/com/andrewbayd/InsertIntoBST.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.andrewbayd;
2+
3+
import com.andrewbayd.datastructures.TreeNode;
4+
5+
/*
6+
You are given the root node of a binary search tree (BST) and a value to insert into the tree.
7+
Return the root node of the BST after the insertion.
8+
9+
https://leetcode.com/problems/insert-into-a-binary-search-tree/
10+
*/
11+
public class InsertIntoBST {
12+
public TreeNode insertIntoBST(TreeNode root, int val) {
13+
if (root == null) return new TreeNode(val);
14+
if (val < root.val) root.left = insertIntoBST(root.left, val);
15+
if (val > root.val) root.right = insertIntoBST(root.right, val);
16+
return root;
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.andrewbayd;
2+
3+
import com.andrewbayd.datastructures.TreeNode;
4+
5+
/*
6+
Given the root of a binary tree, return its maximum depth.
7+
A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
8+
9+
https://leetcode.com/problems/maximum-depth-of-binary-tree/
10+
*/
11+
public class MaxDepthBinaryTree {
12+
public int maxDepth(TreeNode root) {
13+
if (root == null) return 0;
14+
int leftDepth = maxDepth(root.left) ;
15+
int rightDepth = maxDepth(root.right);
16+
return Math.max(leftDepth, rightDepth) + 1;
17+
}
18+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.andrewbayd;
2+
3+
import com.andrewbayd.datastructures.TreeNode;
4+
5+
import java.util.Arrays;
6+
import java.util.Iterator;
7+
8+
/*
9+
Design an algorithm to serialize and deserialize a binary search tree.
10+
11+
https://leetcode.com/problems/serialize-and-deserialize-bst/
12+
*/
13+
14+
public class SerializeDeserializeBST {
15+
16+
// Encodes a tree to a single string.
17+
public String serialize(TreeNode root) {
18+
StringBuilder result = new StringBuilder();
19+
serializeRecursive(root, result);
20+
return result.toString().trim();
21+
}
22+
23+
private void serializeRecursive(TreeNode root, StringBuilder str) {
24+
if (root == null) {
25+
str.append("x ");
26+
return;
27+
}
28+
str.append(root.val).append(" ");
29+
serializeRecursive(root.left, str);
30+
serializeRecursive(root.right, str);
31+
}
32+
33+
// Decodes your encoded data to tree.
34+
public TreeNode deserialize(String data) {
35+
return deserializeRecursive(Arrays.asList(data.split(" ")).iterator());
36+
}
37+
38+
private TreeNode deserializeRecursive(Iterator<String> data) {
39+
String current = data.next();
40+
41+
if (current.equals("x")) {
42+
return null;
43+
}
44+
45+
TreeNode root = new TreeNode(Integer.parseInt(current));
46+
root.left = deserializeRecursive(data);
47+
root.right = deserializeRecursive(data);
48+
49+
return root;
50+
}
51+
}

src/com/andrewbayd/datastructures/TreeNode.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ public class TreeNode {
66
public TreeNode left;
77
public TreeNode right;
88

9-
TreeNode() {
9+
public TreeNode() {
1010
}
1111

12-
TreeNode(int val) {
12+
public TreeNode(int val) {
1313
this.val = val;
1414
}
1515

16-
TreeNode(int val, TreeNode left, TreeNode right) {
16+
public TreeNode(int val, TreeNode left, TreeNode right) {
1717
this.val = val;
1818
this.left = left;
1919
this.right = right;

0 commit comments

Comments
 (0)