• [LeetCode] 687. Longest Univalue Path


    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.

    Note: The length of path between two nodes is represented by the number of edges between them.

    Example 1:

    Input:

                  5
                 / 
                4   5
               /    
              1   1   5
    

    Output:

    2
    

    Example 2:

    Input:

                  1
                 / 
                4   5
               /    
              4   4   5
    

    Output:

    2
    

    Note: The given binary tree has not more than 10000 nodes. The height of the tree is not more than 1000.

    Similar with Binary Tree Max Path Sum,  for a given node t, the longest univalue path of this subtree whose root is t, the longest univalue path either includes t or not include t. If not including t, then the answer is either the longest univalue path of t's left subtree or right subtree. If including t, then the answer will be the sum between the left subtree longest straight path and the right subtree longest straight path. 

    Solution 1. Naive Recursion.

     1 class Solution {
     2     public int longestUnivaluePath(TreeNode root) {
     3         if(root == null) {
     4             return 0;
     5         }
     6         int leftLongestPath = longestUnivaluePath(root.left);
     7         int rightLongestPath = longestUnivaluePath(root.right);
     8         int longestPathWithoutCurrentNode = Math.max(leftLongestPath, rightLongestPath);
     9         int leftLongestSinglePath = longestSinglePath(root.left);
    10         int rightLongestSinglePath = longestSinglePath(root.right);
    11         int longestPathWithCurrentNode = 0;
    12         if(root.left != null && root.left.val == root.val) {
    13             longestPathWithCurrentNode += (leftLongestSinglePath + 1);
    14         }
    15         if(root.right != null && root.right.val == root.val) {
    16             longestPathWithCurrentNode += (rightLongestSinglePath + 1);
    17         }      
    18         return Math.max(longestPathWithoutCurrentNode, longestPathWithCurrentNode);
    19     }
    20     private int longestSinglePath(TreeNode node) {
    21         if(node == null) {
    22             return 0;
    23         }
    24         int leftLongestSinglePath = node.left != null && node.left.val == node.val ? longestSinglePath(node.left) + 1 : 0;
    25         int rightLongestSinglePath = node.right != null && node.right.val == node.val ? longestSinglePath(node.right) + 1 : 0;
    26         return Math.max(leftLongestSinglePath, rightLongestSinglePath);
    27     }
    28 }

    The problem with solution is that when computing longest sing path for a subtree, it does not memoize any smaller subtrees' computation result. It always recursively exhaust all nodes of a subtree to get the longest single path. Since each longestSingePath call recursively calls another two longestSinglePath, the runtime for this method alone is O(2^h), where h is the height of a given subtree. 

    Solution 2. Recursion with memoization when computing longest single path for a subtree.

     1 class Solution {
     2     class ResultType {
     3         int singlePath;
     4         int maxPath;
     5         Integer val;
     6         ResultType(int sp, int mp, Integer v) {
     7             this.singlePath = sp;
     8             this.maxPath = mp;
     9             this.val = v;
    10         }
    11     }
    12     public int longestUnivaluePath(TreeNode root) {
    13         ResultType result = helper(root);
    14         return result.maxPath;
    15     }
    16     private ResultType helper(TreeNode node) {
    17         if(node == null) {
    18             return new ResultType(0, 0, null);
    19         }
    20         ResultType left = helper(node.left);
    21         ResultType right = helper(node.right);
    22         int singlePath = 0;
    23         if(left.val != null && left.val == node.val) {
    24             singlePath = left.singlePath + 1;
    25         }
    26         if(right.val != null && right.val == node.val) {
    27             singlePath = Math.max(singlePath, right.singlePath + 1);
    28         }
    29         int maxPathWithoutCurrentNode = Math.max(left.maxPath, right.maxPath);
    30         int maxPathWithCurrentNode = 0;
    31         if(left.val != null && left.val == node.val) {
    32             maxPathWithCurrentNode += (left.singlePath + 1);
    33         }
    34         if(right.val != null && right.val == node.val) {
    35             maxPathWithCurrentNode += (right.singlePath + 1);
    36         }
    37         int maxPath = Math.max(maxPathWithoutCurrentNode, maxPathWithCurrentNode);
    38         return new ResultType(singlePath, maxPath, node.val);
    39     }
    40 }

    Solution 3. A cleaner implementation with the same memoization idea

     1 class Solution {
     2     int ans = 0;
     3     public int longestUnivaluePath(TreeNode root) {
     4         helper(root);
     5         return ans;
     6     }
     7     private int helper(TreeNode node) {
     8         if(node == null) {
     9             return 0;
    10         }
    11         int leftMaxPath = helper(node.left);
    12         int rightMaxPath = helper(node.right);
    13         int leftMaxPathWithCurrentNode = node.left != null && node.left.val == node.val ? leftMaxPath + 1 : 0;
    14         int rightMaxPathWithCurrentNode = node.right != null && node.right.val == node.val ? rightMaxPath + 1 : 0;
    15         ans = Math.max(ans, leftMaxPathWithCurrentNode + rightMaxPathWithCurrentNode);
    16         return Math.max(leftMaxPathWithCurrentNode, rightMaxPathWithCurrentNode);
    17     }
    18 }
  • 相关阅读:
    Maven+JSP+Servlet+JDBC+Redis+Mysql实现的黑马旅游网
    Thymeleaf+SpringBoot+SpringDataJPA实现的中小医院信息管理系统
    Thymeleaf+SpringBoot+Mybatis实现的家庭财务管理系统
    JSP+Struts2+JDBC+Mysql实现的校园宿舍管理系统
    JSP+SSH+Mysql+C3P0实现的传智播客网上商城
    JSP+SSH+Mysql实现的学生管理系统
    JSP+Servlet+C3P0+Mysql实现的简单新闻系统
    笔记:EF出现列名 'Discriminator' 无效、类没有加入数据库上下文也被数据迁移生成表
    判断字符串中是否存在的几种方案:string.indexof、string.contains、list.contains、list.any几种方式效率对比
    Sql server 中关闭ID自增字段(SQL取消ID自动增长)
  • 原文地址:https://www.cnblogs.com/lz87/p/10222911.html
Copyright © 2020-2023  润新知