-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Create 2331-evaluate-boolean-binary-tree.js #3604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
*/ | ||
var evaluateTree = function(root) { | ||
|
||
const dfs = (node) => { |
There was a problem hiding this comment.
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;
}
There was a problem hiding this comment.
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);
}
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
avoiding nested function.
* @return {boolean} | ||
*/ | ||
var evaluateTree = function(root) { | ||
return (dfs(root) && true) || false; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aakhtar3, any update? Here's the submission link: https://leetcode.com/problems/evaluate-boolean-binary-tree/submissions/1388673250/
There was a problem hiding this comment.
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/
There was a problem hiding this comment.
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/
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Ykhan799, it's done.
Make if statement as one.
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/