From c02c0d7e355feccbbfca3cf23da1d69f1100e195 Mon Sep 17 00:00:00 2001 From: aadil42 <77232799+aadil42@users.noreply.github.com> Date: Mon, 29 Jul 2024 14:20:04 +0530 Subject: [PATCH 1/2] Create 1457-pseudo-palindromic-paths-in-a-binary-tree.js --- ...eudo-palindromic-paths-in-a-binary-tree.js | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 javascript/1457-pseudo-palindromic-paths-in-a-binary-tree.js diff --git a/javascript/1457-pseudo-palindromic-paths-in-a-binary-tree.js b/javascript/1457-pseudo-palindromic-paths-in-a-binary-tree.js new file mode 100644 index 000000000..d81831a93 --- /dev/null +++ b/javascript/1457-pseudo-palindromic-paths-in-a-binary-tree.js @@ -0,0 +1,61 @@ +/** + * + * 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 | Hashing | Backtraking | tree-traversal + * Time O(n) | Space O(n) + * https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/ + * @param {TreeNode} root + * @return {number} + */ +var pseudoPalindromicPaths = function(root) { + + const addNum = (num, hashSet) => { + hashSet[num] = (hashSet[num] + 1) || 1; + } + + const removeNum = (num, hashSet) => { + hashSet[num] = hashSet[num] - 1; + if(hashSet[num] === 0) delete hashSet[num]; + } + + const isPalindrome = (hashSet) => { + + let oddOccurances = 0; + + for(const key in hashSet) { + if(hashSet[key] % 2) oddOccurances++; + } + + return oddOccurances < 2; + } + + const dfs = (node, hashSet) => { + if(!node.left && !node.right && isPalindrome(hashSet)) return 1; + if(!node.left && !node.right) return 0; + + let total = 0; + if(node.left) { + addNum(node.left.val, hashSet); + total += dfs(node.left, hashSet); + removeNum(node.left.val, hashSet); + } + + if(node.right) { + addNum(node.right.val, hashSet); + total += dfs(node.right, hashSet); + removeNum(node.right.val, hashSet); + } + + return total; + } + + return dfs(root, {[root.val]: 1} ); + +}; From 2ea31ec60d3442ecce33c1c73591eae1e0b97c8f Mon Sep 17 00:00:00 2001 From: Yaseen Khan Date: Sun, 13 Oct 2024 10:31:15 -0700 Subject: [PATCH 2/2] Adjusted Spacing --- ...-pseudo-palindromic-paths-in-a-binary-tree.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/javascript/1457-pseudo-palindromic-paths-in-a-binary-tree.js b/javascript/1457-pseudo-palindromic-paths-in-a-binary-tree.js index d81831a93..2e3e5b4fc 100644 --- a/javascript/1457-pseudo-palindromic-paths-in-a-binary-tree.js +++ b/javascript/1457-pseudo-palindromic-paths-in-a-binary-tree.js @@ -22,32 +22,32 @@ var pseudoPalindromicPaths = function(root) { const removeNum = (num, hashSet) => { hashSet[num] = hashSet[num] - 1; - if(hashSet[num] === 0) delete hashSet[num]; + if (hashSet[num] === 0) delete hashSet[num]; } const isPalindrome = (hashSet) => { let oddOccurances = 0; - for(const key in hashSet) { - if(hashSet[key] % 2) oddOccurances++; + for (const key in hashSet) { + if (hashSet[key] % 2) oddOccurances++; } return oddOccurances < 2; } const dfs = (node, hashSet) => { - if(!node.left && !node.right && isPalindrome(hashSet)) return 1; - if(!node.left && !node.right) return 0; + if (!node.left && !node.right && isPalindrome(hashSet)) return 1; + if (!node.left && !node.right) return 0; let total = 0; - if(node.left) { + if (node.left) { addNum(node.left.val, hashSet); total += dfs(node.left, hashSet); removeNum(node.left.val, hashSet); } - if(node.right) { + if (node.right) { addNum(node.right.val, hashSet); total += dfs(node.right, hashSet); removeNum(node.right.val, hashSet); @@ -56,6 +56,6 @@ var pseudoPalindromicPaths = function(root) { return total; } - return dfs(root, {[root.val]: 1} ); + return dfs(root, {[root.val]: 1} \); };