tree functions segregation
This commit is contained in:
@@ -0,0 +1,61 @@
|
|||||||
|
package com.dsa.trees;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Queue;
|
||||||
|
|
||||||
|
public class DepthFirstTraversal {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try (BufferedReader reader = new BufferedReader(new FileReader("input.txt"))) {
|
||||||
|
String[] elements = reader.readLine().trim().split(",");
|
||||||
|
Queue<TreeNode> queue = new LinkedList<>();
|
||||||
|
TreeNode root = new TreeNode(Integer.parseInt(elements[0]));
|
||||||
|
queue.offer(root);
|
||||||
|
int index = 1;
|
||||||
|
while (!queue.isEmpty() && index < elements.length) {
|
||||||
|
TreeNode node = queue.poll();
|
||||||
|
if (!elements[index].equalsIgnoreCase("null")) {
|
||||||
|
node.left = new TreeNode(Integer.parseInt(elements[index]));
|
||||||
|
queue.offer(node.left);
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
if (!elements[index].equalsIgnoreCase("null")) {
|
||||||
|
node.right = new TreeNode(Integer.parseInt(elements[index]));
|
||||||
|
queue.offer(node.right);
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
System.out.println("MinDepth: " + minDepth(root));
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int diameter = 0;
|
||||||
|
public static int diameterOfBinaryTree(TreeNode root) {
|
||||||
|
diameter = 0;
|
||||||
|
if (root == null) return 0;
|
||||||
|
heightOfBT(root);
|
||||||
|
return diameter - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int heightOfBT(TreeNode root) {
|
||||||
|
if (root == null) return 0;
|
||||||
|
int leftHeight = heightOfBT(root.left);
|
||||||
|
int rightHeight = heightOfBT(root.right);
|
||||||
|
diameter = Math.max(leftHeight + rightHeight + 1, diameter);
|
||||||
|
return Math.max(leftHeight, rightHeight) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int minDepth(TreeNode root) {
|
||||||
|
if(root == null) return 0;
|
||||||
|
int leftHeight = minDepth(root.left);
|
||||||
|
int rightHeight = minDepth(root.right);
|
||||||
|
if (leftHeight == 0) return rightHeight + 1;
|
||||||
|
if (rightHeight == 0) return leftHeight + 1;
|
||||||
|
return Math.min(leftHeight, rightHeight) + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -32,7 +32,6 @@ public class LevelOrderTraversal {
|
|||||||
for (List<Integer> level : list) {
|
for (List<Integer> level : list) {
|
||||||
System.out.println(level);
|
System.out.println(level);
|
||||||
}
|
}
|
||||||
System.out.println("MinDepth: " + minDepth(root));
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
@@ -75,29 +74,4 @@ public class LevelOrderTraversal {
|
|||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int diameter = 0;
|
|
||||||
public static int diameterOfBinaryTree(TreeNode root) {
|
|
||||||
diameter = 0;
|
|
||||||
if (root == null) return 0;
|
|
||||||
heightOfBT(root);
|
|
||||||
return diameter - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int heightOfBT(TreeNode root) {
|
|
||||||
if (root == null) return 0;
|
|
||||||
int leftHeight = heightOfBT(root.left);
|
|
||||||
int rightHeight = heightOfBT(root.right);
|
|
||||||
diameter = Math.max(leftHeight + rightHeight + 1, diameter);
|
|
||||||
return Math.max(leftHeight, rightHeight) + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int minDepth(TreeNode root) {
|
|
||||||
if(root == null) return 0;
|
|
||||||
int leftHeight = minDepth(root.left);
|
|
||||||
int rightHeight = minDepth(root.right);
|
|
||||||
if (leftHeight == 0) return rightHeight + 1;
|
|
||||||
if (rightHeight == 0) return leftHeight + 1;
|
|
||||||
return Math.min(leftHeight, rightHeight) + 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user