{"id":489,"date":"2017-10-01T09:42:45","date_gmt":"2017-10-01T16:42:45","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=489"},"modified":"2018-08-31T12:33:24","modified_gmt":"2018-08-31T19:33:24","slug":"leetcode-687-longest-univalue-path","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-687-longest-univalue-path\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 687. Longest Univalue Path"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 687. Longest Univalue Path - \u5237\u9898\u627e\u5de5\u4f5c EP78\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/yX1hVhcHcH8?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><\/p>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u68f5\u4e8c\u53c9\u6811\uff0c\u8fd4\u56de\u4e00\u6761\u6700\u957f\u7684\u8def\u5f84\uff0c\u8981\u6c42\u8def\u5f84\u4e0a\u6240\u6709\u7684\u8282\u70b9\u503c\u90fd\u76f8\u540c\u3002<\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/longest-univalue-path\/description\/\">https:\/\/leetcode.com\/problems\/longest-univalue-path\/description\/<\/a><\/p>\n<p><strong>Problem:\u00a0<\/strong><\/p>\n<p>Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root.<\/p>\n<p><b>Note:<\/b>\u00a0The length of path between two nodes is represented by the number of edges between them.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<p>Input:<\/p>\n<pre class=\"crayon:false\">              5\r\n             \/ \\\r\n            4   5\r\n           \/ \\   \\\r\n          1   1   5\r\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"crayon:false\">2\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<p>Input:<\/p>\n<pre class=\"crayon:false\">              1\r\n             \/ \\\r\n            4   5\r\n           \/ \\   \\\r\n          4   4   5\r\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"crayon:false\">2\r\n<\/pre>\n<p><b>Note:<\/b>\u00a0The given binary tree has not more than 10000 nodes. The height of the tree is not more than 1000.<\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"7983117522\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>DFS<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/687-ep78-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-494\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/687-ep78-1.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/687-ep78-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/687-ep78-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/687-ep78-1-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/687-ep78-1-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<h1><strong>Solution: Recursion<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 69 ms\r\nclass Solution {\r\npublic:\r\n    int longestUnivaluePath(TreeNode* root) {\r\n        if (root == nullptr) return 0;\r\n        int ans = 0;\r\n        univaluePath(root, &amp;ans);\r\n        return ans;\r\n    }\r\nprivate:\r\n    int univaluePath(TreeNode* root, int* ans) {\r\n        if (root == nullptr) return 0;\r\n        int l = univaluePath(root-&gt;left, ans);\r\n        int r = univaluePath(root-&gt;right, ans);\r\n        int pl = 0;\r\n        int pr = 0;\r\n        if (root-&gt;left &amp;&amp; root-&gt;val == root-&gt;left-&gt;val) pl = l + 1;\r\n        if (root-&gt;right &amp;&amp; root-&gt;val == root-&gt;right-&gt;val) pr = r + 1;\r\n        *ans = max(*ans, pl + pr);\r\n        return max(pl, pr);\r\n    }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:java decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 12 ms\r\nclass Solution {\r\n    private int ans;\r\n    public int longestUnivaluePath(TreeNode root) {\r\n        if (root == null) return 0;        \r\n        this.ans = 0;\r\n        univaluePath(root);\r\n        return this.ans;\r\n    }\r\n    \r\n    private int univaluePath(TreeNode root) {\r\n        if (root == null) return 0;\r\n        int l = univaluePath(root.left);\r\n        int r = univaluePath(root.right);\r\n        int pl = 0;\r\n        int pr = 0;\r\n        if (root.left != null &amp;&amp; root.val == root.left.val) pl = l + 1;\r\n        if (root.right != null &amp;&amp; root.val == root.right.val) pr = r + 1;\r\n        this.ans = Math.max(this.ans, pl + pr);\r\n        return Math.max(pl, pr);\r\n    }\r\n}<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:default decode:true\">\"\"\"\r\nAuthor: Huahua\r\nRuntime: 899 ms\r\n\"\"\"\r\nclass Solution:\r\n    def longestUnivaluePath(self, root):\r\n        self.ans = 0\r\n        self._univaluePath(root)\r\n        return self.ans\r\n    \r\n    def _univaluePath(self, root):\r\n        if root is None: return 0\r\n        l = self._univaluePath(root.left) if root.left is not None else -1\r\n        r = self._univaluePath(root.right) if root.right is not None else -1\r\n        pl = l + 1 if l &gt;= 0 and root.val == root.left.val else 0\r\n        pr = r + 1 if r &gt;= 0 and root.val == root.right.val else 0\r\n        self.ans = max(self.ans, pl + pr)\r\n        return max(pl, pr)\r\n<\/pre>\n<\/div><\/div>\n<h1><strong>Related Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-124-binary-tree-maximum-path-sum\/\">\u82b1\u82b1\u9171 LeetCode 124. Binary Tree Maximum Path Sum<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u68f5\u4e8c\u53c9\u6811\uff0c\u8fd4\u56de\u4e00\u6761\u6700\u957f\u7684\u8def\u5f84\uff0c\u8981\u6c42\u8def\u5f84\u4e0a\u6240\u6709\u7684\u8282\u70b9\u503c\u90fd\u76f8\u540c\u3002 https:\/\/leetcode.com\/problems\/longest-univalue-path\/description\/ Problem:\u00a0 Given a binary tree, find the length of the longest path where each node in the path has the same value. This&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[117,177,116,17,28],"class_list":["post-489","post","type-post","status-publish","format-standard","hentry","category-tree","tag-longest","tag-medium","tag-path","tag-recursion","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/489","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=489"}],"version-history":[{"count":15,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/489\/revisions"}],"predecessor-version":[{"id":3790,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/489\/revisions\/3790"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}