Skip to main content

Command Palette

Search for a command to run...

Day 18 of LeetCode Challenge

Published
3 min readView as Markdown
Day 18 of LeetCode Challenge
T

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: Remove Duplicates from Sorted Array

Link to the problem: https://leetcode.com/problems/remove-duplicates-from-sorted-array

class Solution {
    public int removeDuplicates(int[] nums) {
        if (nums.length == 0) return 0;

        int i = 1;

        for (int j = 1; j < nums.length; j++) {
            if (nums[j] != nums[i - 1]) {
                nums[i] = nums[j];
                i++;
            }
        }

        return i;        
    }
}

Problem 2: Diameter of Binary Tree

Link to the problem: https://leetcode.com/problems/diameter-of-binary-tree/

class Solution {
    int res = 0;

    public int diameterOfBinaryTree(TreeNode root) {
        dfs(root);
        return res;        
    }

    private int dfs(TreeNode root) {
        if (root == null) {
            return 0;
        }

        int l = dfs(root.left);
        int r = dfs(root.right);

        res = Math.max(res, l + r);

        return 1 + Math.max(l, r);
    }    
}

Problem 3: Merge Sorted Array

Link to the problem: https://leetcode.com/problems/merge-sorted-array/

class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int i=0;
        int j=0;
        int[] temp = nums1.clone();
        int k=0;
        while(i<m && j<n){
            if(temp[i]<=nums2[j])
                nums1[k++] = temp[i++];
            else
                nums1[k++] = nums2[j++];
        }
        while(i<m)
            nums1[k++] = temp[i++];
        while(j<n)
            nums1[k++] = nums2[j++];
    }
}

Problem 4: Symmetric Tree

Link to the problem: https://leetcode.com/problems/symmetric-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 {
    public boolean isSymmetric(TreeNode root) {
        return isEqual(root.left, root.right, true);
    }
    private boolean isEqual(TreeNode left, TreeNode right, boolean bool){
        if(!bool)   
            return false;
        if(left==null && right == null)
            return bool;
        if((left==null && right!=null) || (left!=null && right==null))
            return false;
        if(left.val!=right.val)
            return false;
        bool = isEqual(left.right, right.left, bool);
        if(!bool)
            return false;
        bool = isEqual(left.left, right.right, bool);
        return bool;
    }
}

Problem 5: Convert Sorted Array to Binary Search Tree

Link to the problem: https://leetcode.com/problems/convert-sorted-array-to-binary-search-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 {
    public TreeNode sortedArrayToBST(int[] nums) {
        return helper(nums, 0, nums.length - 1);
    }

    private TreeNode helper(int[] nums, int left, int right) {
        if (left > right) return null;
        int mid = (left + right) / 2;
        TreeNode root = new TreeNode(nums[mid]);
        root.left = helper(nums, left, mid - 1);
        root.right = helper(nums, mid + 1, right);
        return root;
    }
}

More from this blog

T

Tushar Pant's Blog

178 posts

Welcome to Tushar's Blog! Here, I share my journey in tech, covering DevOps and cloud computing. Explore tutorials, tips, and insights to fuel your learning and growth in technology.