Skip to content

Conversation

aadil42
Copy link
Contributor

@aadil42 aadil42 commented Aug 13, 2024

Solved evaluate-boolean-binary-tree in JS.

File(s) Added: 2331-evaluate-boolean-binary-tree.js
Language(s) Used: JavaScript
Submission URL: https://leetcode.com/problems/evaluate-boolean-binary-tree/submissions/1432550324/

*/
var evaluateTree = function(root) {

const dfs = (node) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should avoid nested functions.

/**
 * Time O(N) | Space O(N)
 * https://leetcode.com/problems/evaluate-boolean-binary-tree/ 
 * @param {TreeNode} root
 * @return {boolean}
 */
var evaluateTree = (root) => {
    const isBaseCase = !(root.left || root.right)
    if (isBaseCase) return root.val;

    return dfs(root);
};

const dfs = (node) => {
    const is2 = (node.val === 2)
    if (is2) return evaluateTree(node.left) || evaluateTree(node.right);

    const is3 = (node.val === 3)
    if (is3) return evaluateTree(node.left) && evaluateTree(node.right);

    return false; 
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you think it's simpler/readable to move the baseCase inside dfs? like so:

var evaluateTree = function(root) {
    return (dfs(root) && true) || false; 
};

const dfs = (node) => {
    if(!node.left && !node.right) return node.val;

    const is2 = (node.val === 2);
    if(is2) return dfs(node.left) || dfs(node.right);
    
    const is3 = (node.val === 3);
    if(is3) return dfs(node.left) && dfs(node.right);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aakhtar3, please check my changes. I think it's simple to move the base-case inside the dfs function. If you want the base-case inside the main function let me know.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There can be multiple base cases.
Ive shown an example to highlight the DFS as a function and base case in the main function to separate how the code would run

* @return {boolean}
*/
var evaluateTree = function(root) {
return (dfs(root) && true) || false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit odd, it would be cleaner if the dfs should return the expected value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this code?

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * DFS | Tree-traversal
 * Time O(n) | Space O(1)
 * https://leetcode.com/problems/evaluate-boolean-binary-tree
 * @param {TreeNode} root
 * @return {boolean}
 */
var evaluateTree = function(root) {
    return dfs(root);
};

const dfs = (node) => {
    if(!node.left && !node.right && node.val) return true;
    if(!node.left && !node.right && !node.val) return false;

    const is2 = (node.val === 2);
    if(is2) return dfs(node.left) || dfs(node.right);
    
    const is3 = (node.val === 3);
    if(is3) return dfs(node.left) && dfs(node.right);

}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aadil42 For lines 22 and 23, you can combine those two lines by using a single if statement to check if not node left and if not node right, then use a ternary to determine true or false depending on the node.val, see code snippet below:

Original:

    if (!node.left && !node.right && node.val) return true;
    if (!node.left && !node.right && !node.val) return false;

New:

    if (!node.left && !node.right) return (node.val) ? true : false;

Here's the submission with the tweak I made: https://leetcode.com/problems/evaluate-boolean-binary-tree/submissions/1426861839/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ykhan799, I think the older version is easier to read the new puts more mental-strain as it has if statement inside if statement. But I updated the code with the new version. Please check Here's the submission link for it: https://leetcode.com/problems/evaluate-boolean-binary-tree/submissions/1431096463/

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aadil42 Fair enough, you can revert the code to have the 2 if statements instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ykhan799, it's done.

@Ykhan799 Ykhan799 merged commit b114d4e into neetcode-gh:main Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants