2024 April 27 Problems

71 views
Skip to first unread message

daryl...@gmail.com

unread,
Apr 27, 2024, 12:41:33 PMApr 27
to leetcode-meetup
Feel free to work on any of the problems you want; we'll have people present their solutions at 11:30.

Please post your solutions to this thread so others can use this as a reference.



Please download and import the following iCalendar (.ics) files to your calendar system.
Weekly: https://us04web.zoom.us/meeting/upIqc-6upj8sGt0O2fJXTw2gC4FxRMQ-iqAT/ics?icsToken=98tyKu6uqT8tHNyRthmOR7YAB4-gKO7xiCldjbcNs03jKRhndVHxFbZkKoBSIZXZ

Join Zoom Meeting
https://us04web.zoom.us/j/76747684609?pwd=HOCapO7jjK0rifPlKvV9CxWFn5lLJn.1

Meeting ID: 767 4768 4609
Passcode: 8wf3Qa

Reminder that we have the monthly meet up for coffee afterwards for anyone attending live.

Flocela Maldonado

unread,
Apr 27, 2024, 1:15:21 PMApr 27
to leetcode-meetup
2331Evaluate Boolean Binary Tree77.7%Easy
Time: O(n). Memory O(lg(n))
class Solution {
public:
bool evaluateTree(TreeNode* root) {

if(root->val == 0)
{
return false;
}
else if(root->val == 1)
{
return true;
}
else if(root->val == 3)
{
bool left = evaluateTree(root->left);
bool right = evaluateTree(root->right);
return left && right;
}
else
{
bool left = evaluateTree(root->left);
bool right = evaluateTree(root->right);
return left || right;
}
}
};

Andriy T

unread,
Apr 27, 2024, 1:20:17 PMApr 27
to leetcode-meetup

Time: O(N) Space: O(lgN)

class Solution {
    public boolean evaluateTree(TreeNode root) {
        if (root.val == 0 || root.val == 1) {
                return root.val == 1;
        }
        else {
                if (root.val == 2) {
                    return evaluateTree(root.left) || evaluateTree(root.right);
                }
                else {
                    return evaluateTree(root.left) && evaluateTree(root.right);
                }
        }
    }
}

Flocela Maldonado

unread,
Apr 27, 2024, 2:35:51 PMApr 27
to leetcode-meetup
First Saturday of the Month is May 4th. Meet up for tea after the Leetcode meeting! That's at MeetFresh (very close to the library)!

Jagrut

unread,
Apr 27, 2024, 3:10:09 PMApr 27
to leetcode-meetup
2331Evaluate Boolean Binary Tree

function evaluateTree (root) {
    let res = true;
    if (root !== null) {
        if (root.val === 0 || root.val === 1) {
            res = root.val === 1;
        }
        else {
            const [left, right] = [evaluateTree(root.left), evaluateTree(root.right)];
            res = (root.val === 2) ? left || right : left && right;
        }
    }
    return res;
};

2673Make Costs of Paths Equal in a Binary Tree

function minIncrements (n, cost) {
    return _min(1, n, cost)[1];
};

function _min(i, n, cost) {
    const res = [0,0];
    if (i <= n) {
        const left = _min(i * 2, n, cost);
        const right = _min(i * 2 + 1, n, cost);
        res[0] = Math.max(left[0], right[0]) + cost[i - 1];
        res[1] = left[1] + right[1] + Math.abs(left[0] - right[0]);
    }
    return res;
}

On Saturday, April 27, 2024 at 9:41:33 AM UTC-7 daryl...@gmail.com wrote:

Flocela Maldonado

unread,
Apr 27, 2024, 4:17:31 PMApr 27
to leetcode-meetup
Time: O(n), Memory O(lg(n))
Would like to try to explain this one. So easy, yet so paradoxical universe.

class Solution {
public:

int returnBranchValue(int index, int& increases, int n, vector<int>& cost)
{
// the children's indexes
int leftIndex = (index * 2) + 1;
int rightIndex = (index * 2) + 2;

if (leftIndex >= n)
{
return cost[index];
}

// never call returnBranchValue on an index that doesn't exist in cost.
int sumLeft = returnBranchValue(leftIndex, increases, n, cost);
int sumRight = returnBranchValue(rightIndex, increases, n, cost);

int diff = std::abs(sumLeft - sumRight);
int max = std::max(sumLeft, sumRight);
increases += diff;

return max + cost[index];
}

int minIncrements(int n, vector<int>& cost) {

int increases = 0;

// Define branch value of a node as the largest sum of a path from that node to one of its leafs. So would have to travel to each leaf and see what the sum is, then the largest of these paths' sums is the node's branch value.
// As we dfs traverse the tree, for the current node return the current node's value plus the larger of the children's branch values. That's this node's branch value.
// At each node there's a sum from the two children's branches. Say one is a larger sum and the other is a smaller sum.
// The smaller branch's value needs to be increased by the difference between the larger branch and the smaller branch.
// So also increase the increases reference variable.
returnBranchValue(0, increases, n, cost);

return increases;
}
};

Ankit Malhotra

unread,
Apr 27, 2024, 7:47:18 PMApr 27
to leetcod...@googlegroups.com



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

if(root.val == 2) {
return evaluateTree(root.left) || evaluateTree(root.right);
}
return evaluateTree(root.left) && evaluateTree(root.right);
}
}

--
whatspp group: http://whatsapp.techbayarea.us/
---
You received this message because you are subscribed to the Google Groups "leetcode-meetup" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leetcode-meet...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/leetcode-meetup/c652c9a2-bee9-403d-b2f0-424bbb042a95n%40googlegroups.com.


--
-Ankit M.

Priyank Gupta

unread,
Apr 27, 2024, 9:20:18 PMApr 27
to leetcode-meetup
class Solution:
def evaluateTree(self, root: Optional[TreeNode]) -> bool:
if root.left == None and root.right == None:
return root.val
if root.val == 2:
return self.evaluateTree(root.left) or self.evaluateTree(root.right)
if root.val == 3:
return self.evaluateTree(root.left) and self.evaluateTree(root.right)

Vivek H

unread,
Apr 29, 2024, 1:06:33 AMApr 29
to leetcod...@googlegroups.com
*******************************************************************************
*******************************************************************************
class Solution {
public:

    int minIncrementsUtil(const vector<int>& cost, int n, int i, int& minInc) {

        if(i >= n )
            return 0;

        int left = minIncrementsUtil(cost, n, 2*i+1, minInc);
        int right = minIncrementsUtil(cost, n, 2*i+2, minInc);

        minInc += abs(left - right);

        return max(left+cost[i], right+cost[i]);

    }
    int minIncrements(int n, vector<int>& cost) {

        int minInc = 0;
        minIncrementsUtil(cost, n, 0, minInc);
        return minInc;
       
    }
};

--

Vivek H

unread,
Apr 29, 2024, 1:14:04 AMApr 29
to leetcod...@googlegroups.com
***************************************************************************
***************************************************************************
class Solution {
public:
    bool evaluateTreeUtil(TreeNode* root) {

        if(!root)
            return 1;
        bool left = evaluateTreeUtil(root->left);
        bool right = evaluateTreeUtil(root->right);
        if(root->val == 2) {
            return left | right;
        } else if(root->val == 3) {
            return left & right;
        } else {
            return (left & right) & root->val;
        }

    }

    bool evaluateTree(TreeNode* root) {

        return evaluateTreeUtil(root);
       
    }
};


On Sat, Apr 27, 2024 at 9:41 AM daryl...@gmail.com <daryl...@gmail.com> wrote:
--

Gowrima Jayaramu

unread,
May 3, 2024, 4:42:34 PMMay 3
to leetcode-meetup
class Solution:
def evaluateTree(self, root: Optional[TreeNode]) -> bool:
if root.val == 0 or root.val == 1:
return root.val
elif root.val == 3:
return self.evaluateTree(root.left) and self.evaluateTree(root.right)
elif root.val == 2:
return self.evaluateTree(root.left) or self.evaluateTree(root.right)

Runtime: 50 ms
Memory: 16.84 MB

Thank you,
gowrima
Reply all
Reply to author
Forward
0 new messages