Skip to main content

Command Palette

Search for a command to run...

Day 15 of LeetCode Challenge

Published
3 min readView as Markdown
Day 15 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: Sum of Root To Leaf Binary Numbers

Link to the problem: https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/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 sumRootToLeaf(TreeNode root) {
        Stack<String> stack = new Stack<>();
        createStack(root, stack, ""); 
        int ans = 0;
        for(String str : stack){
            int num = 0;
            for(int i=0; i<str.length(); i++){
                if(str.charAt(i)=='1')
                    num += Math.pow(2, (str.length()-i-1));
            }
            ans+=num;
        }
        return ans;
    }
    private String createStack(TreeNode root,Stack<String> stack, String s){
        if(root==null)
            return "";
        if(root.left==null && root.right==null){
            s=s.concat(Integer.toString(root.val));
            stack.push(s);
            s = s.substring(0, s.length()-1);
        }
        createStack(root.left, stack, s.concat(Integer.toString(root.val)));
        createStack(root.right, stack, s.concat(Integer.toString(root.val)));
        return "";
    }
}

Problem 2: Same Tree

Link to the problem: https://leetcode.com/problems/same-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 isSameTree(TreeNode p, TreeNode q) {
        if(p==null && q==null)
            return true;
        if((p==null && q!=null) || (p!=null && q==null))
            return false;
        if(p.val!=q.val)
            return false;
        boolean bool = true;
        bool = !bool?bool:isSameTree(p.left, q.left);
        bool = !bool?bool:isSameTree(p.right, q.right);
        return bool;
    }
}

Problem 3: Binary Tree In order Traversal

Link to the problem: https://leetcode.com/problems/binary-tree-inorder-traversal/

/**
 * 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 {
    List<Integer> list = new ArrayList<>();
    public List<Integer> inorderTraversal(TreeNode root) {
        if(root==null)
            return list;
        inorderTraversal(root.left);
        list.add(root.val);
        inorderTraversal(root.right);
        return list;
    }
}

Problem 4: Binary Tree Tilt

Link to the problem: https://leetcode.com/problems/binary-tree-tilt/submissions/1662252199/

/**
 * 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 findTilt(TreeNode root) {
        int ans = 0;
        List<Integer> list = new ArrayList<Integer>();
        calculateTilts(root, list);
        for(int i:list)
            ans+=i;
        return ans;
    }
    private int calculateTilts(TreeNode root, List<Integer> list){
        if(root==null)
            return 0;
        int left = root.left==null?0:root.left.val+calculateTilts(root.left, list);
        int right = root.right==null?0:root.right.val+calculateTilts(root.right, list);
        int num = left>right?left-right:right-left;
        list.add(num);
        return left+right;
    }
}

Problem 5: Root Equals Sum of Children

Link to the problem: https://leetcode.com/problems/root-equals-sum-of-children/

/**
 * 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 checkTree(TreeNode root) {
        if(root.val==root.left.val+root.right.val)
            return true;
        return false;
    }
}

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.