zoukankan      html  css  js  c++  java
  • 【leetcode】最长同值路径

    /*当前节点于上一个相对根节点比较*/
    int recursion(struct TreeNode* node, struct TreeNode* root,int* max){
        if(!node) return 0;
        int left = recursion(node->left,node,max);
        int right = recursion(node->right,node,max);
        if (left+right > *max) *max = left+right;
        return (node->val == root->val)? (left>right)? left+1: right+1 :0;
    }
    int longestUnivaluePath(struct TreeNode* root){
        int max=0;
        recursion(root,root,&max);
        return max;
    }
    /*当前节点于与左右两子节点比较*/
    int longestUnivaluePath(struct TreeNode* root){
        int globle=0;
        UnivaluePath(root,&globle);
        return globle;
    }
    
    int UnivaluePath(struct TreeNode* root,int* globle){
       if(root==NULL){
           return 0;
       } 
        int l=UnivaluePath(root->left,globle);
        int r=UnivaluePath(root->right,globle);
        int left=0;
        int right=0;
        if(root->left&&root->val==root->left->val){
             left=l+1;   
        }
        if(root->right&&root->val==root->right->val){
             right=r+1;   
        }
        *globle=(*globle<left+right)?(left+right):(*globle);
        return left>right?left:right;
    }
  • 相关阅读:
    P1158 导弹拦截
    麦基数(p1045)
    Django之路由层
    web应用与http协议
    Django之简介
    Mysql之表的查询
    Mysql之完整性约束
    Mysql之常用操作
    Mysql之数据类型
    Mysql之数据库简介
  • 原文地址:https://www.cnblogs.com/ganxiang/p/13711370.html
Copyright © 2011-2022 走看看