{"id":6817,"date":"2020-05-23T22:40:04","date_gmt":"2020-05-24T05:40:04","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6817"},"modified":"2020-05-24T00:14:00","modified_gmt":"2020-05-24T07:14:00","slug":"leetcode-1457-pseudo-palindromic-paths-in-a-binary-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/uncategorized\/leetcode-1457-pseudo-palindromic-paths-in-a-binary-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1457. Pseudo-Palindromic Paths in a Binary Tree"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1457. Pseudo-Palindromic Paths in a Binary Tree - \u5237\u9898\u627e\u5de5\u4f5c EP329\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/Ia2OAm9OzP0?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Given a binary tree where node values are digits from 1 to 9. A path in the binary tree is said to be&nbsp;<strong>pseudo-palindromic<\/strong>&nbsp;if at least one permutation of the node values in the path is a palindrome.<\/p>\n\n\n\n<p><em>Return the number of&nbsp;<strong>pseudo-palindromic<\/strong>&nbsp;paths going from the root node to leaf nodes.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/05\/06\/palindromic_paths_1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [2,3,1,3,1,null,1]\n<strong>Output:<\/strong> 2 \n<strong>Explanation:<\/strong> The figure above represents the given binary tree. There are three paths going from the root node to leaf nodes: the red path [2,3,3], the green path [2,1,1], and the path [2,3,1]. Among these paths only red path and green path are pseudo-palindromic paths since the red path [2,3,3] can be rearranged in [3,2,3] (palindrome) and the green path [2,1,1] can be rearranged in [1,2,1] (palindrome).\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/05\/07\/palindromic_paths_2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [2,1,1,1,3,null,null,null,null,null,1]\n<strong>Output:<\/strong> 1 \n<strong>Explanation:<\/strong> The figure above represents the given binary tree. There are three paths going from the root node to leaf nodes: the green path [2,1,1], the path [2,1,3,1], and the path [2,1]. Among these paths only the green path is pseudo-palindromic since [2,1,1] can be rearranged in [1,2,1] (palindrome).\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [9]\n<strong>Output:<\/strong> 1\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The&nbsp;given binary tree will have between&nbsp;<code>1<\/code>&nbsp;and&nbsp;<code>10^5<\/code>&nbsp;nodes.<\/li><li>Node values are digits from&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>9<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Counting<\/strong><\/h2>\n\n\n\n<p>At most one number can occur odd times.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/05\/1457-ep329-1.png\" alt=\"\" class=\"wp-image-6822\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/05\/1457-ep329-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/05\/1457-ep329-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/05\/1457-ep329-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/05\/1457-ep329-2.png\" alt=\"\" class=\"wp-image-6823\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/05\/1457-ep329-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/05\/1457-ep329-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/05\/1457-ep329-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n) \/ stack size<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int pseudoPalindromicPaths (TreeNode* root) {\n    vector<int> counts(10);\n    function<int(TreeNode*)> dfs = [&](TreeNode* node) {\n      if (!node) return 0;\n      ++counts[node->val];\n      int c = 0;\n      if (!node->left && !node->right) {\n        int odds = 0;\n        for (int i = 1; i <= 9; ++i)\n          if (counts[i] &#038; 1) ++odds;\n        if (odds <= 1) c = 1;\n      }\n      int l = dfs(node->left);\n      int r = dfs(node->right);      \n      --counts[node->val];\n      return c + l + r;\n    };\n    return dfs(root);\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p>Use a binary string to represent occurrences of each number (even: 0 \/ odd: 1), we can use xor to flip the bit.<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int pseudoPalindromicPaths(TreeNode* root, int s = 0) {    \n    if (!root) return 0;\n    s ^= (1 << root->val);\n    if (!root->left && !root->right)\n      return __builtin_popcount(s) <= 1;\n    return pseudoPalindromicPaths(root->left, s) \n         + pseudoPalindromicPaths(root->right, s);          \n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary tree where node values are digits from 1 to 9. A path in the binary tree is said to be&nbsp;pseudo-palindromic&nbsp;if at least&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6817","post","type-post","status-publish","format-standard","hentry","category-uncategorized","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6817","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6817"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6817\/revisions"}],"predecessor-version":[{"id":6824,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6817\/revisions\/6824"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}