From 1dd4a529fcc7e7d9fb2de170fbd628d9f2af6f7f Mon Sep 17 00:00:00 2001 From: Kelvin Date: Thu, 29 Jul 2021 13:50:56 -0400 Subject: [PATCH] =?UTF-8?q?Update=200513.=E6=89=BE=E6=A0=91=E5=B7=A6?= =?UTF-8?q?=E4=B8=8B=E8=A7=92=E7=9A=84=E5=80=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更改变量名称 --- problems/0513.找树左下角的值.md | 34 +++++++++++--------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/problems/0513.找树左下角的值.md b/problems/0513.找树左下角的值.md index e83fcc18..27c6e83c 100644 --- a/problems/0513.找树左下角的值.md +++ b/problems/0513.找树左下角的值.md @@ -280,31 +280,25 @@ Python: class Solution: def findBottomLeftValue(self, root: TreeNode) -> int: max_depth = -float("INF") - max_left_value = -float("INF") - - def __traversal(root, left_len): - nonlocal max_depth, max_left_value + leftmost_val = 0 + def __traverse(root, cur_depth): + nonlocal max_depth, leftmost_val if not root.left and not root.right: - if left_len > max_depth: - max_depth = left_len - max_left_value = root.val - return - + if cur_depth > max_depth: + max_depth = cur_depth + leftmost_val = root.val if root.left: - left_len += 1 - __traversal(root.left, left_len) - left_len -= 1 - + cur_depth += 1 + __traverse(root.left, cur_depth) + cur_depth -= 1 if root.right: - left_len += 1 - __traversal(root.right, left_len) - left_len -= 1 - return - - __traversal(root, 0) + cur_depth += 1 + __traverse(root.right, cur_depth) + cur_depth -= 1 - return max_left_value + __traverse(root, 0) + return leftmost_val ``` **迭代 - 层序遍历** ```python