Leetcode104. 二叉树的最大深度--深度优先搜索

it2026-09-29  8

题目

# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def maxDepth(self, root: TreeNode) -> int: if root==None: return 0; depth=0 maxdep=0 stk=[] p=root while stk or p: while p: depth+=1 stk.append((p,depth)) p=p.left temp=stk.pop() p=temp[0] depth=temp[1] if maxdep<depth: maxdep=depth p=p.right return maxdep
最新回复(0)