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

Binary Tree diameter I

没有weight的二叉树直径, 直接用node的高度计算直径

PreviousZigZag Order traversalNextMaximum Path Sum Binary Tree

Last updated 4 years ago

Was this helpful?

laicode:

Leetcode:

Given a binary tree in which each node contains an integer number. The diameter is defined as the longest distance from one leaf node to another leaf node. The distance is the number of nodes on the path.

If there does not exist any such paths, return 0.

Examples

5

/ \

2 11

/ \

6 14

The diameter of this tree is 4 (2 → 5 → 11 → 14)

How is the binary tree represented?

We use the level order traversal sequence with a special symbol "#" denoting the null node.

For Example:

The sequence [1, 2, 3, #, #, 4] represents the following binary tree:

1

/ \

2 3

/

4

想法: top-down+ bottom up

  1. 遍历tree的每一个node

  2. 在recursion里面: 输入: 下一个child node, 输出: 那个node的高度

    1. 在当前的node里面从left, right children得到对应的高度

    2. 把left,right的高度相加再+1 当前的node得到当前subtree 的直径

    3. 把当前的直径和global max的直径对比,更新global max 直径

    4. 返回当前的node的高度,即max(left, right) +1

class Solution(object):
  def diameter(self, root):
    """
    input: TreeNode root
    return: int
    """
    # write your solution here
    #
    # 1.confirm question
    #  tree diameter is the longest path from one leaf node to another leaf node in the tree
    #  Assumption/Base case 
    #  
    # 3. method 1: use global max 
    # 4. debug
    #   test case:
    #         1
    #   None    2
    #        None 3
    #           5   4
    # the diameter is 3:   5->3->4
    #
    # 5. TIme complexity: O(n) since we need to go through every node to check the height and update max_path
    #   space compelxity: O(h)
    if not root :
      return 0
    self.max_path = 0
    height = self.get_diameter(root)
    return self.max_path 

  def get_diameter(self, node):
    height =0
    # base case when cur node is none
    if not node:
      return height
    # get height
    left_h = self.get_diameter(node.left)
    right_h = self.get_diameter(node.right)
    # update height of current node
    #
    height = max(left_h,right_h) +1
    if left_h != 0 and right_h != 0:
      self.max_path = max(self.max_path , left_h + right_h + 1)
    return height

    
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution(object):
  def diameter(self, root):
    """
    input: TreeNode root
    return: int
    """
    # write your solution here
    # 1.confirm question
    #  tree diameter is the longest path from one leaf node to another leaf node in the tree
    #  Assumption/Base case 
    #   1. when tree has root node only: return 0
    #   2. when root node has only one node and that child node doesn't have children node-> return 0 since there is no path
    #
    # 2. input: root node of tree node, output : the length of tree diameter
    #
    # 3. idea:
    #  1. check if node dosen't exists, return 0
    #  2. use recursion to iterate each node in tree
    #     in each node, use get_height to obtain the max heigh of two children  of current node
    #     sum up two heights + 1 to get the path length and compare it with the max path length from two children node
    #     return the maximum length
    #  3. base case: when node is none, then return height 0, max_path = 0
    #     base case, when node is not none, it contain only one children, then return height of children+1,  max_path from children
    #     base case, when node and its two children nodes are not none,
    #     then compute max_path = heightof left children + height of right children  + 1 and compare it with max_path from 
    #    its children nodes
    #
    # 4. debug
    #   test case:
    #         1
    #   None    2
    #        None 3
    #           5   4
    # the diameter is 3:   5->3->4
    #
    # 5. TIme complexity: O(n) since we need to go through every node to check the height and update max_path
    #   space compelxity: O(h)
    if not root:
      return 0
    height, max_path = self.get_diameter(root)
    return max_path

  def get_diameter(self, node):
    height, max_path =0,0
    # base case when cur node is none
    if not node:
      return height, max_path
    # get height
    left_h, left_path = self.get_diameter(node.left)
    right_h, right_path = self.get_diameter(node.right)
    # update height of current node
    height = max(left_h,right_h) +1
    #when path exist,children node are not none, then update max path 
    if right_h!= 0 and left_h != 0:
      max_path = left_h + right_h +1
    # update diameter by comparing the max_path from left, right and current node
    max_path = max(max_path, left_path,right_path)
        
    return height, max_path
https://app.laicode.io/app/problem/142
https://blog.csdn.net/xuchonghao/article/details/80657610