Day 14 of LeetCode Challenge

Cloud and DevOps Engineer with hands-on expertise in AWS, CI/CD pipelines, Docker, Kubernetes, and Monitoring tools. Adept at building and automating scalable, fault-tolerant cloud infrastructures, and consistently improving system performance, security, and reliability in dynamic environments.
Problem 1: Range Sum of BST
Link to the problem: https://leetcode.com/problems/range-sum-of-bst/description/

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int rangeSumBST(TreeNode root, int low, int high) {
int ans = 0;
Stack<Integer> stack = new Stack<>();
Traverse(root, stack, low, high);
for(int i:stack)
ans+=i;
return ans;
}
private TreeNode Traverse(TreeNode root, Stack<Integer> stack, int low, int high){
if(root==null)
return null;
Traverse(root.left, stack, low, high);
if(root.val>=low && root.val<=high)
stack.push(root.val);
Traverse(root.right, stack, low, high);
return root;
}
}
Problem 2: Maximum Depth of a Binary Tree
Link to the problem: https://leetcode.com/problems/maximum-depth-of-binary-tree/description/

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if(root==null)
return 0;
int left_height = maxDepth(root.left);
int right_height = maxDepth(root.right);
return left_height>right_height?left_height+1:right_height+1;
}
}
Problem 3: SubTree of Another Tree
Link to the problem: https://leetcode.com/problems/subtree-of-another-tree/

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
}
if (p == null || q == null) {
return false;
}
if (p.val != q.val) {
return false;
}
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
private boolean helper(TreeNode p, TreeNode q) {
if (p == null) {
return false;
}
if (isSameTree(p, q)) {
return true;
}
return helper(p.left, q) || helper(p.right, q);
}
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
return helper(root, subRoot);
}
}
Problem 4: Cousins in Binary Tree
Link to the problem: https://leetcode.com/problems/cousins-in-binary-tree/

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
static int[] arr = new int[2];
public boolean isCousins(TreeNode root, int x, int y) {
int x_depth = findDepth(root, x, 0, 0);
int y_depth = findDepth(root, y, 0, 1);
x_depth = arr[0];
y_depth = arr[1];
int x_parent = findParent(root, x, 0);
int y_parent = findParent(root, y, 1);
System.out.println(arr[0]+" "+arr[1]);
if(x_depth==y_depth && arr[0]!=arr[1])
return true;
return false;
}
private int findDepth(TreeNode root, int x, int depth, int num){
if(root==null)
return -1;
if(root.val==x){
arr[num] = depth+1;
}
int left_depth = findDepth(root.left, x, depth+1, num);
int right_depth = findDepth(root.right, x, depth+1, num);
return left_depth>right_depth?left_depth+1:right_depth+1;
}
private int findParent(TreeNode root, int x, int num){
if(root==null)
return 0;
if((root.left!=null && root.left.val==x) || (root.right!=null && root.right.val==x)){
arr[num]=root.val;
return 0;
}
findParent(root.left, x, num);
findParent(root.right, x, num);
return 0;
}
}
Problem 5: Minimum depth of a Binary Tree
Link to the problem: https://leetcode.com/problems/minimum-depth-of-binary-tree/description/

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int minDepth(TreeNode root) {
Stack<Integer> stack = new Stack<>();
findDepths(root, stack, 0);
int ans = Integer.MAX_VALUE;
for(int i:stack)
ans=ans<i?ans:i;
if(ans==Integer.MAX_VALUE)
ans=0;
return ans;
}
private int findDepths(TreeNode root, Stack stack, int depth){
if(root==null)
return 0;
if(root.left==null && root.right==null)
stack.push(depth+1);
else
findDepths(root.left, stack, depth+1);
findDepths(root.right, stack, depth+1);
return 0;
}
}




