Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the Binary Trees section #27

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/binarytrees/AllOrderTraversals.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ public class AllOrderTraversals {
static class Pair {

int count = 0;

TreeNode node = null;

public Pair(int count, TreeNode node) {
Pair(int count, TreeNode node) {

this.count = count;

this.node = node;
}
}
Expand Down
38 changes: 38 additions & 0 deletions src/binarytrees/FindPathsToLeaves.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package binarytrees;

import java.util.LinkedList;
import java.util.List;

import binarytrees.CustomBinaryTree.TreeNode;

// Problem Link: https://leetcode.com/problems/binary-tree-paths/
// Solution Link: https://leetcode.com/problems/binary-tree-paths/solutions/68258/accepted-java-simple-solution-in-8-lines/

public class FindPathsToLeaves {

private List<String> paths;

public List<String> compute(TreeNode root) {

paths = new LinkedList<>();

if (root == null) return paths;

traverse(root, "");

return paths;
}

private void traverse(TreeNode node, String path) {

TreeNode left = node.left;
TreeNode right = node.right;

path += node.value;

if (left == null && right == null) paths.add(path);

if (left != null) traverse(left, path + "->");
if (right != null) traverse(right, path + "->");
}
}
29 changes: 29 additions & 0 deletions src/binarytrees/GetFlipEquivalence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package binarytrees;

import binarytrees.CustomBinaryTree.TreeNode;

// Problem Link: https://leetcode.com/problems/flip-equivalent-binary-trees/
// Solution Link: https://leetcode.com/problems/flip-equivalent-binary-trees/solutions/200514/java-python-3-dfs-3-liners-and-bfs-with-explanation-time-space-o-n/

// Approach: Check BOTH Sub-Trees [LEFT & RIGHT] for both UNFLIPPED & FLIPPED Cases
// Time Complexity: O(N^2) for 4 Sub-Problems; but O(N) for UNIQUE Numbers in Nodes
// Space Complexity: O(H) for PERFECT Binary Tree; else O(N) for SKEWED Binary Tree

public class GetFlipEquivalence {

public boolean compute(TreeNode root1, TreeNode root2) {

if (root1 == null) return (root2 == null);
if (root2 == null) return (root1 == null);

if (root1.value != root2.value) return false;

boolean unflipped = (compute(root1.left, root2.left) &&
compute(root1.right, root2.right));

boolean flipped = (compute(root1.left, root2.right) &&
compute(root1.right, root2.left));

return (unflipped || flipped);
}
}
41 changes: 41 additions & 0 deletions src/binarytrees/GetTargetNodePath.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package binarytrees;

import java.util.LinkedList;
import java.util.List;

import binarytrees.CustomBinaryTree.TreeNode;

// Problem Link: https://www.codingninjas.com/codestudio/problems/3843990
// Solution Link: https://takeuforward.org/data-structure/print-root-to-node-path-in-a-binary-tree/

// Approach: Use Recursive Technique; Time Complexity: O(N), Space Complexity: O(N)

public class GetTargetNodePath {

private List<Integer> path;

public List<Integer> compute(TreeNode root, int x) {

path = new LinkedList<>();

travel(root, x);

return path;
}

private boolean travel(TreeNode node, int x) {

if (node == null) return false;

path.add(node.value);

if (node.value == x) return true;

if (travel(node.left, x)) return true;
if (travel(node.right, x)) return true;

path.remove(path.size() - 1);

return false;
}
}
78 changes: 78 additions & 0 deletions src/binarytrees/MaximumTreeWidth.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package binarytrees;

import java.util.LinkedList;
import java.util.Queue;

import binarytrees.CustomBinaryTree.TreeNode;

// Problem Link: https://leetcode.com/problems/maximum-width-of-binary-tree/
// Solution Link: https://leetcode.com/problems/maximum-width-of-binary-tree/solutions/106653/java-one-queue-solution-with-hashmap/comments/492554

// Approach: Use Iterative Technique; Time Complexity: O(N), Space Complexity: O(N)

public class MaximumTreeWidth {

static class Pair {

int index = 0;
TreeNode node = null;

Pair(int index, TreeNode node) {

this.index = index;
this.node = node;
}
}

private Queue<Pair> nodes;

public int compute(TreeNode root) {

int width = 0;

if (root == null) return width;

nodes = new LinkedList<>();

nodes.offer(new Pair(0, root));

while (!nodes.isEmpty()) {

int capacity = nodes.size();

int mark = 0, low = 0, high = 0;

while ((mark += 1) <= capacity) {

Pair current = nodes.poll();

int index = current.index;
TreeNode node = current.node;

TreeNode left = node.left;
TreeNode right = node.right;

if (mark == 1) low = index;
if (mark == capacity) high = index;

if (left != null) {

int child = (index * 2) + 1;

nodes.offer(new Pair(child, left));
}

if (right != null) {

int child = (index * 2) + 2;

nodes.offer(new Pair(child, right));
}
}

width = Math.max(width, high - low + 1);
}

return width;
}
}
71 changes: 71 additions & 0 deletions src/binarytrees/NodesAtDistanceK.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package binarytrees;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import binarytrees.CustomBinaryTree.TreeNode;

// Problem Link: https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/
// Solution Link: https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/solutions/143798/1ms-beat-100-simple-java-dfs-with-without-hashmap-including-explanation/

// Approach: Use Recursive Technique; Time Complexity: O(N), Space Complexity: O(N + N)

public class NodesAtDistanceK {

private Map<TreeNode, Integer> map;
private List<Integer> nodes;

public List<Integer> compute(TreeNode root, TreeNode target, int k) {

map = new HashMap<>();
nodes = new LinkedList<>();

search(root, target);
enlist(root, k, map.get(root));

return nodes;
}

private int search(TreeNode node, TreeNode target) {

if (node == null) return -1;

if (node == target) {

map.put(node, 0);
return 0;
}

int left = search(node.left, target);

if (left >= 0) {

map.put(node, left + 1);
return left + 1;
}

int right = search(node.right, target);

if (right >= 0) {

map.put(node, right + 1);
return right + 1;
}

return -1;
}

private void enlist(TreeNode node, int k, int length) {

if (node == null) return;

length = map.getOrDefault(node, length);

if (length == k) nodes.add(node.value);

enlist(node.left, k, length + 1);
enlist(node.right, k, length + 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,20 @@

import binarytrees.CustomBinaryTree.TreeNode;

// Problem Link: https://practice.geeksforgeeks.org/problems/mirror-tree/1
// Problem Link: https://leetcode.com/problems/invert-binary-tree/
// Solution Link: https://discuss.geeksforgeeks.org/comment/8b09ac7d06f57dcbcb742b5f26da3152

// Approch Link: SWAP the LEFT with RIGHT Sub-Trees in BOTTOM-UP Method
// Time Complexity: O(N); Space Complexity: O(N) in the Recursion Space

public class BuildTreeReflection {
public class ReflectBinaryTree {

public void compute(TreeNode node) {

node = mirror(node);
}

private TreeNode mirror(TreeNode root) {
public TreeNode compute(TreeNode root) {

if (root == null) return null;

TreeNode left = mirror(root.left);
TreeNode right = mirror(root.right);
TreeNode left = compute(root.left);
TreeNode right = compute(root.right);

root.left = right;
root.right = left;
Expand Down
71 changes: 71 additions & 0 deletions src/binarytrees/ViewTopAndBottom.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package binarytrees;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.TreeMap;

import binarytrees.CustomBinaryTree.TreeNode;

// Problem [Top] Link: https://www.codingninjas.com/codestudio/problems/799401
// Problem [Bottom] Link: https://www.codingninjas.com/codestudio/problems/893110

// Solution [Top] Link: https://takeuforward.org/data-structure/top-view-of-a-binary-tree/
// Solution [Bottom] Link: https://takeuforward.org/data-structure/bottom-view-of-a-binary-tree/

// Approach: Traverse the Nodes LEVEL-wise and maintain a MAP of the Node VALUES w.r.t. the Axis INDICES

public class ViewTopAndBottom {

static class Pair {

int axis = 0;
TreeNode node = null;

Pair(int axis, TreeNode node) {

this.axis = axis;
this.node = node;
}
}

private Map<Integer, Integer> lines;
private Queue<Pair> nodes;

public List<Integer> compute(TreeNode root) {

if (root == null) return new LinkedList<>();

// SORT Map, according
// to the Axis INDICES
lines = new TreeMap<>();
nodes = new LinkedList<>();

return watch(root);
}

private List<Integer> watch(TreeNode root) {

// Set off the QUEUE with the ROOT
nodes.add(new Pair(0, root));

while (!nodes.isEmpty()) {

Pair current = nodes.poll();

int axis = current.axis;
TreeNode node = current.node;

TreeNode left = node.left, right = node.right;

lines.putIfAbsent(axis, node.value); // To view from TOP
lines.put(axis, node.value); // When viewing from BOTTOM

if (left != null) nodes.offer(new Pair(axis - 1, left));
if (right != null) nodes.offer(new Pair(axis + 1, right));
}

return new LinkedList<>(lines.values());
}
}