some logical improvements
This commit is contained in:
@@ -24,7 +24,7 @@ public class DepthFirstTraversal {
|
|||||||
queue.offer(node.left);
|
queue.offer(node.left);
|
||||||
}
|
}
|
||||||
index++;
|
index++;
|
||||||
if (!elements[index].equalsIgnoreCase("null")) {
|
if (index < elements.length && !elements[index].equalsIgnoreCase("null")) {
|
||||||
node.right = new TreeNode(Integer.parseInt(elements[index]));
|
node.right = new TreeNode(Integer.parseInt(elements[index]));
|
||||||
queue.offer(node.right);
|
queue.offer(node.right);
|
||||||
}
|
}
|
||||||
@@ -43,27 +43,33 @@ public class DepthFirstTraversal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static int diameter = 0;
|
private static int diameter = 0;
|
||||||
|
|
||||||
public static int diameterOfBinaryTree(TreeNode root) {
|
public static int diameterOfBinaryTree(TreeNode root) {
|
||||||
diameter = 0;
|
diameter = 0;
|
||||||
if (root == null) return 0;
|
if (root == null)
|
||||||
|
return 0;
|
||||||
heightOfBT(root);
|
heightOfBT(root);
|
||||||
return diameter - 1;
|
return diameter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int heightOfBT(TreeNode root) {
|
public static int heightOfBT(TreeNode root) {
|
||||||
if (root == null) return 0;
|
if (root == null)
|
||||||
|
return 0;
|
||||||
int leftHeight = heightOfBT(root.left);
|
int leftHeight = heightOfBT(root.left);
|
||||||
int rightHeight = heightOfBT(root.right);
|
int rightHeight = heightOfBT(root.right);
|
||||||
diameter = Math.max(leftHeight + rightHeight + 1, diameter);
|
diameter = Math.max(leftHeight + rightHeight, diameter);
|
||||||
return Math.max(leftHeight, rightHeight) + 1;
|
return Math.max(leftHeight, rightHeight) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int minDepth(TreeNode root) {
|
public static int minDepth(TreeNode root) {
|
||||||
if(root == null) return 0;
|
if (root == null)
|
||||||
|
return 0;
|
||||||
int leftHeight = minDepth(root.left);
|
int leftHeight = minDepth(root.left);
|
||||||
int rightHeight = minDepth(root.right);
|
int rightHeight = minDepth(root.right);
|
||||||
if (leftHeight == 0) return rightHeight + 1;
|
if (leftHeight == 0)
|
||||||
if (rightHeight == 0) return leftHeight + 1;
|
return rightHeight + 1;
|
||||||
|
if (rightHeight == 0)
|
||||||
|
return leftHeight + 1;
|
||||||
return Math.min(leftHeight, rightHeight) + 1;
|
return Math.min(leftHeight, rightHeight) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,28 +78,36 @@ public class DepthFirstTraversal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static int balancedDepth(TreeNode root) {
|
public static int balancedDepth(TreeNode root) {
|
||||||
if (root == null) return 0;
|
if (root == null)
|
||||||
|
return 0;
|
||||||
int leftHeight = balancedDepth(root.left);
|
int leftHeight = balancedDepth(root.left);
|
||||||
int rightHeight = balancedDepth(root.right);
|
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);
|
int weight = Math.abs(leftHeight - rightHeight);
|
||||||
if (weight > 1) return -1;
|
if (weight > 1)
|
||||||
|
return -1;
|
||||||
return Math.max(leftHeight, rightHeight) + 1;
|
return Math.max(leftHeight, rightHeight) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean hasPathSum(TreeNode root, int targetSum) {
|
public static boolean hasPathSum(TreeNode root, int targetSum) {
|
||||||
if(root == null) return false;
|
if (root == null)
|
||||||
if (root.left == null && root.right == null && root.val == targetSum) return true;
|
return false;
|
||||||
|
if (root.left == null && root.right == null && root.val == targetSum)
|
||||||
|
return true;
|
||||||
int requiredNewSum = targetSum - root.val;
|
int requiredNewSum = targetSum - root.val;
|
||||||
boolean hasPathSumLeft = hasPathSum(root.left, requiredNewSum);
|
boolean hasPathSumLeft = hasPathSum(root.left, requiredNewSum);
|
||||||
if (hasPathSumLeft) return true;
|
if (hasPathSumLeft)
|
||||||
|
return true;
|
||||||
return hasPathSum(root.right, requiredNewSum);
|
return hasPathSum(root.right, requiredNewSum);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<Integer> pathTracer = new LinkedList<>();
|
private static List<Integer> pathTracer = new LinkedList<>();
|
||||||
private static List<List<Integer>> allPaths = new LinkedList<>();
|
private static List<List<Integer>> allPaths = new LinkedList<>();
|
||||||
|
|
||||||
public static List<List<Integer>> pathSum(TreeNode root, int targetSum) {
|
public static List<List<Integer>> pathSum(TreeNode root, int targetSum) {
|
||||||
if (root == null) return allPaths;
|
if (root == null)
|
||||||
|
return allPaths;
|
||||||
pathTracer.add(root.val);
|
pathTracer.add(root.val);
|
||||||
if (root.left == null && root.right == null && root.val == targetSum) {
|
if (root.left == null && root.right == null && root.val == targetSum) {
|
||||||
allPaths.add(new LinkedList<>(pathTracer));
|
allPaths.add(new LinkedList<>(pathTracer));
|
||||||
@@ -106,18 +120,23 @@ public class DepthFirstTraversal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static int maxSum;
|
private static int maxSum;
|
||||||
|
|
||||||
public static int maxPathSum(TreeNode root) {
|
public static int maxPathSum(TreeNode root) {
|
||||||
if (root != null) maxSum = root.val;
|
if (root != null)
|
||||||
|
maxSum = root.val;
|
||||||
int maxSumInternal = maxPathSumInternal(root);
|
int maxSumInternal = maxPathSumInternal(root);
|
||||||
return Math.max(maxSumInternal, maxSum);
|
return Math.max(maxSumInternal, maxSum);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int maxPathSumInternal(TreeNode root) {
|
private static int maxPathSumInternal(TreeNode root) {
|
||||||
if (root == null) return 0;
|
if (root == null)
|
||||||
|
return 0;
|
||||||
int leftMaxSum = maxPathSumInternal(root.left);
|
int leftMaxSum = maxPathSumInternal(root.left);
|
||||||
int rightMaxSum = maxPathSumInternal(root.right);
|
int rightMaxSum = maxPathSumInternal(root.right);
|
||||||
maxSum = Math.max(leftMaxSum + rightMaxSum + root.val, maxSum); //is max including current node in path
|
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
|
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);
|
maxSum = Math.max(pathMax, maxSum);
|
||||||
return pathMax;
|
return pathMax;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user