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

    和求直径的那个题思路差不多,不过这个题要判断是不是相等。

    public int longestUnivaluePath(TreeNode root) {
            int res = 0;
            if(root==null) return 0;
            if (root.left!=null&&root.left.val==root.val)
                res = 1+helper(root.left);
            if (root.right!=null&&root.right.val==root.val)
                res+=1+helper(root.right);
            return Math.max(res,Math.max(longestUnivaluePath(root.left),longestUnivaluePath(root.right)));
        }
        //用来判断相同数字深度
        public int helper(TreeNode root)
            {
                if (root==null) return 0;
                int cur = 0;
                    if (root.left!=null&&root.val==root.left.val)
                        cur = 1+helper(root.left);
                if (root.right!=null&&root.val==root.right.val)
                    cur = Math.max(cur,1+helper(root.right));
                return cur;
            }
  • 相关阅读:
    Codeforces 385C
    Codeforces 496C
    HDU 6114 Chess
    Codeforces 839B
    Codeforces 483B
    Codeforces 352B
    Codeforces 768B
    Codeforces 38B
    Codeforces 735B
    Codeforces 534B
  • 原文地址:https://www.cnblogs.com/stAr-1/p/8397421.html
Copyright © 2011-2022 走看看