From 49d55fb1c8c26f668c1676075ac10a21b527ab92 Mon Sep 17 00:00:00 2001 From: Kelvin Date: Thu, 29 Jul 2021 11:25:33 -0400 Subject: [PATCH 1/2] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20513.=E6=89=BE=E6=A0=91?= =?UTF-8?q?=E5=B7=A6=E4=B8=8B=E8=A7=92=E7=9A=84=E5=80=BC=20python=E9=83=A8?= =?UTF-8?q?=E5=88=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 上一版本 递归python代码跟题解的方式不同. 个人认为不同方法做出的代码没有必要放在题解里, 建议修改成我提供的python版本. 2. 增加Python3 迭代部分的代码. --- problems/0513.找树左下角的值.md | 66 +++++++++++++++++++------- 1 file changed, 48 insertions(+), 18 deletions(-) diff --git a/problems/0513.找树左下角的值.md b/problems/0513.找树左下角的值.md index 17d15fde..e83fcc18 100644 --- a/problems/0513.找树左下角的值.md +++ b/problems/0513.找树左下角的值.md @@ -274,27 +274,57 @@ class Solution { Python: + +**递归 - 回溯** ```python -//递归法 -# Definition for a binary tree node. -# class TreeNode: -# def __init__(self, val=0, left=None, right=None): -# self.val = val -# self.left = left -# self.right = right class Solution: def findBottomLeftValue(self, root: TreeNode) -> int: - depth=0 - self.res=[] - def level(root,depth): - if not root:return - if depth==len(self.res): - self.res.append([]) - self.res[depth].append(root.val) - level(root.left,depth+1) - level(root.right,depth+1) - level(root,depth) - return self.res[-1][0] + max_depth = -float("INF") + max_left_value = -float("INF") + + def __traversal(root, left_len): + nonlocal max_depth, max_left_value + + if not root.left and not root.right: + if left_len > max_depth: + max_depth = left_len + max_left_value = root.val + return + + if root.left: + left_len += 1 + __traversal(root.left, left_len) + left_len -= 1 + + if root.right: + left_len += 1 + __traversal(root.right, left_len) + left_len -= 1 + return + + __traversal(root, 0) + + return max_left_value +``` +**迭代 - 层序遍历** +```python +class Solution: + def findBottomLeftValue(self, root: TreeNode) -> int: + queue = deque() + if root: + queue.append(root) + result = 0 + while queue: + q_len = len(queue) + for i in range(q_len): + if i == 0: + result = queue[i].val + cur = queue.popleft() + if cur.left: + queue.append(cur.left) + if cur.right: + queue.append(cur.right) + return result ``` Go: From 1dd4a529fcc7e7d9fb2de170fbd628d9f2af6f7f Mon Sep 17 00:00:00 2001 From: Kelvin Date: Thu, 29 Jul 2021 13:50:56 -0400 Subject: [PATCH 2/2] =?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