Skip to content

Commit

Permalink
added to DepthFirstSearch
Browse files Browse the repository at this point in the history
  • Loading branch information
kdn251 committed Feb 14, 2017
1 parent 77e804a commit ebfdc70
Show file tree
Hide file tree
Showing 6 changed files with 291 additions and 0 deletions.
42 changes: 42 additions & 0 deletions DepthFirstSearch/balancedBinaryTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Given a binary tree, determine if it is height-balanced.

// For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {

boolean balanced = true;

public boolean isBalanced(TreeNode root) {

height(root);
return balanced;

}

private int height(TreeNode root) {

if(root == null) return 0;

int leftHeight = height(root.left);
int rightHeight = height(root.right);

if(Math.abs(leftHeight - rightHeight) > 1) {

balanced = false;

}

return 1 + Math.max(leftHeight, rightHeight);

}

}
43 changes: 43 additions & 0 deletions DepthFirstSearch/convertSortedArrayToBinarySearchTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {

public TreeNode sortedArrayToBST(int[] nums) {

if(nums.length == 0) return null;

TreeNode root = helper(nums, 0, nums.length - 1);

return root;

}

private TreeNode helper(int[] nums, int start, int end) {

if(start <= end) {

int mid = (start + end) / 2;

TreeNode current = new TreeNode(nums[mid]);

current.left = helper(nums, start, mid - 1);
current.right = helper(nums, mid + 1, end);

return current;

}

return null;

}

}
24 changes: 24 additions & 0 deletions DepthFirstSearch/maximumDepthOfABinaryTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Given a binary tree, find its maximum depth.

// The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {

public int maxDepth(TreeNode root) {

if(root == null) return 0;

return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));

}

}
71 changes: 71 additions & 0 deletions DepthFirstSearch/numberOfIslands.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

// Example 1:

// 11110
// 11010
// 11000
// 00000
// Answer: 1

// Example 2:

// 11000
// 11000
// 00100
// 00011
// Answer: 3

public class Solution {

char[][] gridCopy;

public int numIslands(char[][] grid) {

//set grid copy to the current grid
gridCopy = grid;

//initialize number of islands to zero
int numberOfIslands = 0;

//iterate through every index of the grid
for(int i = 0; i < grid.length; i++) {

for(int j = 0; j < grid[0].length; j++) {

//attempt to "sink" the current index of the grid
numberOfIslands += sink(gridCopy, i, j);

}

}

//return the total number of islands
return numberOfIslands;

}

int sink(char[][] grid, int i, int j) {

//check the bounds of i and j and if the current index is an island or not (1 or 0)
if(i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == '0') {

return 0;

}

//set current index to 0
grid[i][j] = '0';

// sink all neighbors of current index
sink(grid, i + 1, j);
sink(grid, i - 1, j);
sink(grid, i, j + 1);
sink(grid, i, j - 1);

//increment number of islands
return 1;

}

}
81 changes: 81 additions & 0 deletions DepthFirstSearch/populatingNextRightPointersInEachNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Given a binary tree

// struct TreeLinkNode {
// TreeLinkNode *left;
// TreeLinkNode *right;
// TreeLinkNode *next;
// }
// Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.

// Initially, all next pointers are set to NULL.

// Note:

// You may only use constant extra space.
// You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
// For example,
// Given the following perfect binary tree,
// 1
// / \
// 2 3
// / \ / \
// 4 5 6 7
// After calling your function, the tree should look like:
// 1 -> NULL
// / \
// 2 -> 3 -> NULL
// / \ / \
// 4->5->6->7 -> NULL

/**
* Definition for binary tree with next pointer.
* public class TreeLinkNode {
* int val;
* TreeLinkNode left, right, next;
* TreeLinkNode(int x) { val = x; }
* }
*/
public class Solution {

public void connect(TreeLinkNode root) {

if(root == null) return;

Queue<TreeLinkNode> queue = new LinkedList<TreeLinkNode>();

queue.add(root);

while(!queue.isEmpty()) {

Queue<TreeLinkNode> currentLevel = new LinkedList<TreeLinkNode>();

TreeLinkNode temp = null;

while(!queue.isEmpty()) {

TreeLinkNode current = queue.remove();
current.next = temp;
temp = current;


if(current.right != null) {

currentLevel.add(current.right);

}

if(current.left!= null) {

currentLevel.add(current.left);
}


}

queue = currentLevel;

}

}

}
30 changes: 30 additions & 0 deletions DepthFirstSearch/sameTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Given two binary trees, write a function to check if they are equal or not.

// Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {

public boolean isSameTree(TreeNode p, TreeNode q) {

if(p == null && q == null) return true;

if(p == null && q != null || q == null && p != null) return false;

if(p.val != q.val) return false;

return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);



}

}

0 comments on commit ebfdc70

Please sign in to comment.