zoukankan      html  css  js  c++  java
  • LC 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.

    Runtime: 72 ms, faster than 28.47% of C++ online submissions for Longest Univalue Path.

    对于这种不经过root的求和题往往都需要一个临时变量,然后考虑一下根节点和子节点的关系,用一个引用得到最优解。

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
      int longestUnivaluePath(TreeNode* root) {
        int ret = 0, tmpret = 0;
        helper(root,tmpret, ret);
        return ret == 0 ? 0 : ret - 1;
      }
      int helper(TreeNode* root, int& tmpret, int& ret){
        if(!root) return 0;
        int rl = helper(root->left, tmpret, ret);
        int rr = helper(root->right, tmpret, ret);
        if(!root->left && !root->right) {
          tmpret = 1;
          //ret = 1;
          return 1;
        } else if(!root->left && root->right){
          if(root->val == root->right->val){
            tmpret = max(tmpret, rr + 1);
            ret = max(ret, tmpret);
            return 1+rr;
          } else return 1;
        } else if(root->left && !root->right){
          if(root->val == root->left->val){
            tmpret = max(tmpret, 1+rl);
            ret = max(ret, tmpret);
            return 1+rl;
          } else return 1;
        } else {
          if(root->val == root->left->val && root->val == root->right->val){
            tmpret = max(tmpret, 1+rr + rl);  
            ret = max(ret, tmpret);
            return 1+max(rr,rl);
          } else if (root->val == root->left->val){
            tmpret = max(tmpret, 1+rl);
            ret = max(ret, tmpret);
            return 1+rl;
          } else if (root->val == root->right->val){
            tmpret = max(tmpret, 1+rr);
            ret = max(ret, tmpret);
            return 1+rr;
          } else return 1;
        }
      }
    };
  • 相关阅读:
    JAVA GUI设
    3.4 jmu-java-随机数-使用蒙特卡罗法计算圆周率的值 (10 分)
    问题:关于2.3 jmu-Java-02基本语法-03-身份证排序 (9 分)
    关于3.1 jmu-Java-03面向对象基础-01-构造函数与toString (3 分)
    linux vim文件编辑的常用命令
    linux的常用命令
    linux文件存储方式
    第一个java
    hdu 2795
    hdu 1394
  • 原文地址:https://www.cnblogs.com/ethanhong/p/10177254.html
Copyright © 2011-2022 走看看