From 01fb704cb6e921a94777f1a6279837532d20891b Mon Sep 17 00:00:00 2001 From: Sri Harsha Damarla Date: Tue, 23 Jun 2026 11:11:46 +0530 Subject: [PATCH] some logical improvements --- src/com/dsa/trees/DepthFirstTraversal.java | 57 ++++++++++++++-------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/src/com/dsa/trees/DepthFirstTraversal.java b/src/com/dsa/trees/DepthFirstTraversal.java index a4b9a78..0a42f0e 100644 --- a/src/com/dsa/trees/DepthFirstTraversal.java +++ b/src/com/dsa/trees/DepthFirstTraversal.java @@ -24,7 +24,7 @@ public class DepthFirstTraversal { queue.offer(node.left); } index++; - if (!elements[index].equalsIgnoreCase("null")) { + if (index < elements.length && !elements[index].equalsIgnoreCase("null")) { node.right = new TreeNode(Integer.parseInt(elements[index])); queue.offer(node.right); } @@ -43,27 +43,33 @@ public class DepthFirstTraversal { } private static int diameter = 0; + public static int diameterOfBinaryTree(TreeNode root) { diameter = 0; - if (root == null) return 0; + if (root == null) + return 0; heightOfBT(root); - return diameter - 1; + return diameter; } public static int heightOfBT(TreeNode root) { - if (root == null) return 0; + if (root == null) + return 0; int leftHeight = heightOfBT(root.left); int rightHeight = heightOfBT(root.right); - diameter = Math.max(leftHeight + rightHeight + 1, diameter); + diameter = Math.max(leftHeight + rightHeight, diameter); return Math.max(leftHeight, rightHeight) + 1; } public static int minDepth(TreeNode root) { - if(root == null) return 0; + 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; + if (leftHeight == 0) + return rightHeight + 1; + if (rightHeight == 0) + return leftHeight + 1; return Math.min(leftHeight, rightHeight) + 1; } @@ -72,28 +78,36 @@ public class DepthFirstTraversal { } public static int balancedDepth(TreeNode root) { - if (root == null) return 0; + if (root == null) + return 0; int leftHeight = balancedDepth(root.left); int rightHeight = balancedDepth(root.right); - if (leftHeight == -1 || rightHeight == -1) return -1; + if (leftHeight == -1 || rightHeight == -1) + return -1; int weight = Math.abs(leftHeight - rightHeight); - if (weight > 1) return -1; + if (weight > 1) + return -1; return Math.max(leftHeight, rightHeight) + 1; } public static boolean hasPathSum(TreeNode root, int targetSum) { - if(root == null) return false; - if (root.left == null && root.right == null && root.val == targetSum) return true; + if (root == null) + return false; + if (root.left == null && root.right == null && root.val == targetSum) + return true; int requiredNewSum = targetSum - root.val; boolean hasPathSumLeft = hasPathSum(root.left, requiredNewSum); - if (hasPathSumLeft) return true; + if (hasPathSumLeft) + return true; return hasPathSum(root.right, requiredNewSum); } private static List pathTracer = new LinkedList<>(); private static List> allPaths = new LinkedList<>(); + public static List> pathSum(TreeNode root, int targetSum) { - if (root == null) return allPaths; + if (root == null) + return allPaths; pathTracer.add(root.val); if (root.left == null && root.right == null && root.val == targetSum) { allPaths.add(new LinkedList<>(pathTracer)); @@ -106,18 +120,23 @@ public class DepthFirstTraversal { } private static int maxSum; + public static int maxPathSum(TreeNode root) { - if (root != null) maxSum = root.val; + if (root != null) + maxSum = root.val; int maxSumInternal = maxPathSumInternal(root); return Math.max(maxSumInternal, maxSum); } private static int maxPathSumInternal(TreeNode root) { - if (root == null) return 0; + if (root == null) + return 0; int leftMaxSum = maxPathSumInternal(root.left); int rightMaxSum = maxPathSumInternal(root.right); - maxSum = Math.max(leftMaxSum + rightMaxSum + root.val, maxSum); //is max including current node in path - int pathMax = Math.max(leftMaxSum + root.val, Math.max(rightMaxSum + root.val, root.val)); //for moving up just return one of max sum to parent + maxSum = Math.max(leftMaxSum + rightMaxSum + root.val, maxSum); // is max including current node in path + int pathMax = Math.max(leftMaxSum + root.val, Math.max(rightMaxSum + root.val, root.val)); // for moving up just + // return one of max + // sum to parent maxSum = Math.max(pathMax, maxSum); return pathMax; }