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
  • 1.Link
  • 2. 题目描述
  • 输入
  • 返回值
  • 输入
  • 返回值
  • 3. 思路
  • 4.Coding

Was this helpful?

  1. DataStructure
  2. Tree

Find Tree height (general iteration method)

PreviousTreeNextCheck BST and Check CompleteTree

Last updated 4 years ago

Was this helpful?

1.Link

2. 题目描述

求给定二叉树的最大深度,最大深度是指树的根结点到最远叶子结点的最长路径上结点的数量。 示例1

输入

复制

{1,2}

返回值

复制

2

示例2

输入

复制

{1,2,3,4,#,#,5}

返回值

复制

3

3. 思路

  1. bottom-up method:

    1. 每个node的input: node, ouput: max_depth of current node

    2. 如果node是none,return 0。否则从left, right children获取 max_depth values, 然后返回 Max(left depth, right depth) +1

  2. top down method:

    1. 每个node的input: node, depth of parent ouput: None

    2. 用global的max_depth 存放结果

    3. 如果node是none,return, 否则 max_depth = max(max depth, depth + 1)

4.Coding

Recursion method

# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

#
# 
# @param root TreeNode类 
# @return int整型
#
class Solution:
    def maxDepth(self , root ):
        # write code here
        #idea: bottom-up, post order 
        # input: node
        # output: max depth from current node
        # 1. base case:  if node == none, return max_depth = 0
        # 2. go to left, right subtree to obtain max depth from left, right
        # 3. return max depth of left/right +1, including current parent node
        #
        if not root:
            return 0
        l_max = self.maxDepth(root.left)
        r_max = self.maxDepth(root.right)
        return max(l_max, r_max) + 1

Iteration DFS method

class Solution:
    def maxDepth(self , root ):
        #
        # DFS iteration 写法
        # iteration method: use array to simulate stack
        # recursion 传进来的参数都 写到stack的tuple里面
        #
        #
        if not root:
            return 0
        depth = 0
        stack = [(root,0, 1 )]
        max_depth = 0
        #l_max = 0
        #r_max = 0
        while stack:
            # input to child node in one level = parent node, count of parent
            # other input parameter: depth
            node, cnt,depth = stack.pop(-1)
            if cnt == 0:
                #preorder
                stack.append((node, cnt+1, depth))
                # always go to left child first, in DFS
                if node.left:
                    # input to next level
                    stack.append((node.left, 0,depth+1))
            if cnt ==1:
                #in-order
                # when cnt =1, then we can take the result from left node
                stack.append((node, cnt+1, depth))
                if node.right:
                    # input to next level
                    stack.append((node.right, 0, depth+1))
            if cnt ==2:
                # post-order back , exit tree
                # global max method
                max_depth = max(max_depth,depth)
                
        return max_depth

Tree 的iteration general的方法/DFS + 每一层都有返回的写法

  1. 在原来的 iteration method for Tree traversal 的方法上添加以下两条

    1. 对于每一层的node的输入,可以在 stack append (node.left, cnt, other inputs)的tuple里面给 child node进行input的添加

    2. 对于每个node的返回值,可以用一个result stack存放子节点的返回值

    3. 在post-order 的地方进行返回操作, 如果current node 有right node就pop result stack的最后一个value,同理如果有left node,就pop最后一个value。 注意: 一定是先pop right再pop left,因为left的result是先比right的res 储存

  2. 简单来说, input 就是在cnt=0 或1的情况 stack.append()的tuple里面添加input。 而output就是在post-order里面从result stack里面pop值出来更新当前的node的result,再append进去返回。


class Solution:
    def maxDepth(self , root ):
        #
        # DFS iteration 写法
        # iteration method: use array to simulate stack
        # recursion 传进来的参数都 写到stack的tuple里面
        #
        #
        if not root:
            return 0
        depth = 0
        stack = [(root,0, 1 )]
        max_depth = 0
        # result stack
        res_stack =[]
        #l_max = 0
        #r_max = 0
        while stack:
            # input to child node in one level = parent node, count of parent
            # other input parameter: depth
            node, cnt,depth = stack.pop(-1)
            if cnt == 0:
                #preorder
                stack.append((node, cnt+1, depth))
                # always go to left child first, in DFS
                if node.left:
                    # input to next level, the third value in tuple
                    # is input to the left child
                    stack.append((node.left, 0,depth))
            if cnt ==1:
                #in-order
                # when cnt =1, then we can take the result from left node
                stack.append((node, cnt+1, depth))
                if node.right:
                    # input to next level
                    stack.append((node.right, 0, depth))
            if cnt ==2:
                # post-order back , exit tree node
                
                # Return result to stack
                # default value =0
                r_res = 0
                l_res = 0
                if res_stack and node.right:
                    r_res = res_stack.pop(-1)
                if res_stack and node.left:
                    l_res = res_stack.pop(-1)
                # append result of this node to result stack
                depth = max(l_res, r_res) +1
                res_stack.append(depth)
                
                # global max method
                #max_depth = max(max_depth,depth)
        return res_stack[-1]

Iteration BFS method/ Level order method

class Solution:
    def maxDepth(self , root ):
        #
        # BFS iteration 写法
        # iteration method: use array to simulate queue
        # 每一层的level都+= 1
        #
        #
        if not root:
            return 0
        queue = [root] # store node in current level
        level = [] # store the node in the next level
        depth = 0
        while queue:
            node = queue.pop(0)
            if node.left:
                level.append(node.left)
            if node.right:
                level.append(node.right)
            if not queue:
                depth += 1
                queue = level
                level = []
        return depth

二叉树的最大深度_牛客题霸_牛客网
Logo