LeetCode-Notes
  • Introduction
  • Records of Practice
  • 关于Github 不支持密码问题
  • 面试题
    • 搜索广告
    • 多模态大模型
    • 刷题记录
  • 算法代码实现
  • Python
    • Python 笔记
  • Spark
    • PySpark
    • Spark Issues
    • Spark调优笔记
  • FeatureEngineering
    • Feature Cleaning
    • Feature Selection
    • Feature Transformation
    • Feature Crossing
  • Recommendation Algorithm
    • Recall-and-PreRank
      • Non-Negative Matrix Fatorization(NMF)
      • Fatorization Machine(FM)
      • User-base/Item-base实现
      • 多路召回实现
    • Ranking
      • NeuralFM
      • DeepFM
      • Deep&Cross network (DCN)
    • DeepLearning-Basic
      • Attention
      • Dropout
      • Batch Norm
  • Machine Learning
    • XGBoost
    • Cross Entropy Loss
    • Other models
  • Graph Neural Network
    • GNN-1-Basic
  • Big Data
    • Reservoir Sampling
  • SQL
    • SQL and PySpark functions
    • Query Film Infomation
    • Create, Insert and Alter Actor Table
    • Manage Employment Data
    • Manage Employment Data -2
  • DataStructure
    • Searching
      • Find power
      • 2 Sum All Pair II
      • Two Sum
      • Search in Rotate Array
      • Search In Shifted Sorted Array II
      • Search in 2D array
      • Three Sum with duplicated values
      • Median of Two Sorted Arrays
    • Array
      • Longest Consecutive Subarray
      • Merge Two Array in-place
      • Trapping water
      • Rotate matrix
    • Sorting
      • Merge intervals
      • 排序
      • 最小的k个数
      • Find TopK largest- QuickSelect快速选择 method
      • MergeSort Linkedlist
      • 第K大元素
    • LinkedList
      • Reverse LinkedList I
      • Reverse K-group linked list
      • Detect Start of Cycle
      • HasCycle
      • DetectCycle II
      • 链表的共同节点
      • 链表中倒数第k个节点
      • 删除链表倒数第k个节点
      • 合并两个链表
      • 在排序数组中查找元素的第一个和最后一个位置
      • 删除链表里面重复的元素-1
    • Tree
      • Find Tree height (general iteration method)
      • Check BST and Check CompleteTree
      • ZigZag Order traversal
      • Binary Tree diameter I
      • Maximum Path Sum Binary Tree
      • Maximum Path Sum Binary Tree II
      • Binary Tree Path Sum To Target III
      • Tree diameter 树的直径II
      • Tree ReConstruction
      • Check if B is Subtree of A
      • The Kth smallest in Binary Search Tree
      • 打印Tree的右视图
      • 二叉搜索树的后序遍历序列
      • 重建二叉树
      • 判断二叉树是否对称
      • Path Sum to Target in Binary Tree
      • Tree-PreOrder-InOrder-PostOrder
    • Heap&Queue
      • Top-K smallest
      • 滑动窗口最大值
      • Find the K-Largest
    • 合并k个已排序的链表
    • String
      • Reverse String
      • 最长不含重复字符的子字符串
      • 最长回文串
      • 最长回文子序列-DP
    • DFS/BFS
      • Number of island
      • Number of Provinces
      • All Permutations of Subsets without duplication
      • All Permutations of Subsets with duplication
      • Combinations Of Coins
      • All Subset I (without fixing size of subset, without order, without duplication)
      • All Subset of K size without duplication II
      • All Subset of K size III (with duplication without considering order)
      • All Permutation II (with duplication and consider order)
      • Factor Combination-质数分解
    • DynamicProgramming
      • DP-解题过程
      • Find Continuous Sequence Sum to Target
      • 1800. Maximum Ascending Subarray Sum
      • NC91 最长上升子序列
      • 查找string的编码方式个数
      • Maximum Product
      • Longest Common Substring
      • Longest Common Substring-II
      • minEditCost
      • Backpack I
      • Array Hopper I
      • Minimum distance between strings
      • 最大正方形
  • Big Data Algorithms
    • Big Data Processing Algorithms
      • Reservior Sampling
      • Shuffle
      • MapReduce
      • Bloom Filter
      • BitMap
      • Heap For Big Data
Powered by GitBook
On this page
  • 题目描述
  • 输入
  • 返回值

Was this helpful?

  1. DataStructure
  2. Tree

Tree diameter 树的直径II

这道题相对于binary tree diameter I + maximum path sum binary tree II +图的DFS

PreviousBinary Tree Path Sum To Target IIINextTree ReConstruction

Last updated 4 years ago

Was this helpful?

牛客:

题目描述

给定一棵树,求出这棵树的直径,即树上最远两点的距离。包含n个结点,n-1条边的连通图称为树。 示例1的树如下图所示。其中4到5之间的路径最长,是树的直径,距离为5+2+4=11 示例1

输入

复制

6,[[0,1],[1,5],[1,2],[2,3],[2,4]],[3,4,2,1,5]

返回值

复制

11

2.思路

maximum path sum binary tree II的思路 + dfs的多个branch的tree的思路

1通过dictionary建树,key为parent,value= list of (child node, edge weight) pair

2.遍历tree的每一个node,找到每个branch的从上往下加起来的max path sum,找到max path sum最大的两个branch,并把两个branch的max path sum相加得到跨越两个branch的path的pathsum, 然后和global max path sum对比更新

3返回找到的最大的从上往下的max path sum + current node的值得到目前最大的path sum

    
class Solution:
    def solve(self , n , Tree_edge , Edge_value ):
        # write code here
        # 1. input: Graph: n= node values, Tree_edge  = list of node to node link/edge, edge_value:weight
        # 2. find max path sum of any two node in tree (maximum path sum binary tree)
        # idea: 
        #  1. build tree using dictionary
        #     key = node value,  value = children node of key
        #  2. find max path sum of any two node in tree, recursion tree, but not binary tree
        #     idea: max path sum  of a node =  max( max path sum from left tree,
        #                                        max path sum from right tree, 
        #                                        max path sum from left + right + node value tree, )
        #   if max path sum from left/ right top-down consecutive path  < 0, set it to 0 
        #    otherwise  add it to the path sum
        #   compare the path sum found in current node with global max
        #   return max top-down path sum including cur node value
        #
        #
        # test case
        if n <=1:
            return None
        tree = {}
        self.global_max = -float('inf')
        # build
        for i in range(len(Tree_edge)):
            if Tree_edge[i].start not in tree.keys():
                tree[Tree_edge[i].start] = []
            if Tree_edge[i].end not in tree.keys():
                tree[Tree_edge[i].end] = []
            # append children and weight to parent node's list 
            node, w = Tree_edge[i].end, Edge_value[i]
            tree[Tree_edge[i].start].append([node,w])
            # 搭建无向图,所以把 end 为key也加上去
            # 加了之后为了防止有loop, 在dfs里面要家 child != parent 的条件
            # 如果不加end的key到dictionary,可以不加child != parent的条件
            tree[Tree_edge[i].end].append([Tree_edge[i].start,w])
        # find max path sum
        root = Tree_edge[0].start
        self.findmaxsum(tree, root,-1)
        return self.global_max
    
    def findmaxsum(self, tree, node,parent):
        # tree: dictionary
        # node: key value in dicionary
        # return max top-down path sum 
        if node not in tree.keys():
            return 0
        path_sum = 0
        ret_path_sum = 0
        # iterate children
        path_ls = [0,0]
        for i in range(len(tree[node])):
            
            node_val = tree[node][i][0]
            node_weight = tree[node][i][1]
            if node_val == parent:
                continue
            p_sum = max(self.findmaxsum(tree, node_val,node)+node_weight, 0)
            ret_path_sum = max(ret_path_sum, p_sum)
            # compute path sum across two branches
            if p_sum > path_ls[0]:
                if p_sum > path_ls[1]:
                    path_ls[0] = path_ls[1]
                    path_ls[1] = p_sum
                else:
                    path_ls[0] = p_sum
        path_sum = sum(path_ls)
        # update global max
        self.global_max = max(self.global_max, path_sum)
        return path_ls[1]
        
         
            
https://www.nowcoder.com/practice/a77b4f3d84bf4a7891519ffee9376df3?tpId=196&tqId=37158&rp=1&ru=%2Factivity%2Foj&qru=%2Fta%2Fjob-code-total%2Fquestion-ranking&tab=answerKey