zoukankan      html  css  js  c++  java
  • 687. Longest Univalue Path

    /**
     * 687. Longest Univalue Path(最长的相同值路经)
     * https://leetcode.com/problems/longest-univalue-path/description/
     * https://www.youtube.com/watch?v=yX1hVhcHcH8
     * */
    class TreeNode(var `val`: Int) {
        var left: TreeNode? = null
        var right: TreeNode? = null
    }
    
    class Solution {
        var result = 0
        fun longestUnivaluePath(root: TreeNode?): Int {
            if (root == null) {
                return 0
            }
            helper(root)
            return result
        }
    
        private fun helper(root: TreeNode?): Int {
            if (root == null) {
                return 0
            }
            //求出单边的univaluePath
            val l = helper(root.left)
            val r = helper(root.right)
            var pl = 0
            var pr = 0
            if (root.left != null && root.`val` == root.left?.`val`) {
                pl = l + 1
            }
            if (root.right != null && root.`val` == root.right?.`val`) {
                pr = r + 1
            }
            result = Math.max(result, pl + pr)
            //返回单边最大的
            return Math.max(pl, pr)
        }
    }
  • 相关阅读:
    SQL高级应用
    li元素之间产生间隔
    js array
    js高阶函数汇总
    git学习记录
    static和assets的区别
    router-link
    vue 创建项目 create和init
    vue的store状态管理模式
    vue中的各种属性
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/12154489.html
Copyright © 2011-2022 走看看