From b4e21799796b4b60fe0a777761aa7d1f1ef94f1b Mon Sep 17 00:00:00 2001 From: Asterisk <44215173+casnz1601@users.noreply.github.com> Date: Mon, 4 Oct 2021 19:25:58 +0800 Subject: [PATCH 1/3] =?UTF-8?q?Update=200404.=E5=B7=A6=E5=8F=B6=E5=AD=90?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 进一步讨论迭代顺序可能性,实际提交显示前序遍历和后序遍历同样可以AC --- problems/0404.左叶子之和.md | 37 +++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/problems/0404.左叶子之和.md b/problems/0404.左叶子之和.md index e55981e2..19149285 100644 --- a/problems/0404.左叶子之和.md +++ b/problems/0404.左叶子之和.md @@ -42,7 +42,7 @@ if (node->left != NULL && node->left->left == NULL && node->left->right == NULL) ## 递归法 -递归的遍历顺序为后序遍历(左右中),是因为要通过递归函数的返回值来累加求取左叶子数值之和。。 +递归的遍历顺序为后序遍历(左右中),是因为要通过递归函数的返回值来累加求取左叶子数值之和。(前序遍历其实也同样AC) 递归三部曲: @@ -230,8 +230,8 @@ class Solution { ## Python -**递归** -```python +> 递归后序遍历 +```python3 class Solution: def sumOfLeftLeaves(self, root: TreeNode) -> int: if not root: @@ -246,9 +246,36 @@ class Solution: return cur_left_leaf_val + left_left_leaves_sum + right_left_leaves_sum ``` +> 递归前序遍历 +```python3 +# 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 sumOfLeftLeaves(self, root: TreeNode) -> int: + # 需要通过中节点来判断其的左节点是否存在;左节点自己的左右节点也是否存在 -**迭代** -```python + if not root: return 0 + + # 初始化left_leaf备用 + left_leaf = 0 + # 若当前节点的左孩子就是左叶子 + if root.left and not root.left.left and not root.left.right: + left_leaf = root.left.val + + left_left_leaves_sum = self.sumOfLeftLeaves(root.left) + right_left_leaves_sum = self.sumOfLeftLeaves(root.right) + + + return left_leaf + left_left_leaves_sum + right_left_leaves_sum +``` + + +> 迭代 +```python3 class Solution: def sumOfLeftLeaves(self, root: TreeNode) -> int: """ From 5e5b3d5f4c926e80e3bad9916f9d13264ff45082 Mon Sep 17 00:00:00 2001 From: Asterisk <44215173+casnz1601@users.noreply.github.com> Date: Fri, 8 Oct 2021 13:12:08 +0800 Subject: [PATCH 2/3] =?UTF-8?q?Update=200404.=E5=B7=A6=E5=8F=B6=E5=AD=90?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 勘误python注释 --- problems/0404.左叶子之和.md | 35 ++++---------------------------- 1 file changed, 4 insertions(+), 31 deletions(-) diff --git a/problems/0404.左叶子之和.md b/problems/0404.左叶子之和.md index 19149285..fb574794 100644 --- a/problems/0404.左叶子之和.md +++ b/problems/0404.左叶子之和.md @@ -171,10 +171,10 @@ class Solution { int rightValue = sumOfLeftLeaves(root.right); // 右 int midValue = 0; - if (root.left != null && root.left.left == null && root.left.right == null) { // 中 + if (root.left != null && root.left.left == null && root.left.right == null) { midValue = root.left.val; } - int sum = midValue + leftValue + rightValue; + int sum = midValue + leftValue + rightValue; // 中 return sum; } } @@ -242,37 +242,10 @@ class Solution: cur_left_leaf_val = 0 if root.left and not root.left.left and not root.left.right: - cur_left_leaf_val = root.left.val # 中 + cur_left_leaf_val = root.left.val - return cur_left_leaf_val + left_left_leaves_sum + right_left_leaves_sum + return cur_left_leaf_val + left_left_leaves_sum + right_left_leaves_sum # 中 ``` -> 递归前序遍历 -```python3 -# 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 sumOfLeftLeaves(self, root: TreeNode) -> int: - # 需要通过中节点来判断其的左节点是否存在;左节点自己的左右节点也是否存在 - - if not root: return 0 - - # 初始化left_leaf备用 - left_leaf = 0 - # 若当前节点的左孩子就是左叶子 - if root.left and not root.left.left and not root.left.right: - left_leaf = root.left.val - - left_left_leaves_sum = self.sumOfLeftLeaves(root.left) - right_left_leaves_sum = self.sumOfLeftLeaves(root.right) - - - return left_leaf + left_left_leaves_sum + right_left_leaves_sum -``` - > 迭代 ```python3 From d131731015f2184cb4ffb428bf414e258225fd61 Mon Sep 17 00:00:00 2001 From: Asterisk <44215173+casnz1601@users.noreply.github.com> Date: Fri, 8 Oct 2021 13:15:58 +0800 Subject: [PATCH 3/3] =?UTF-8?q?Update=200404.=E5=B7=A6=E5=8F=B6=E5=AD=90?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 勘误python注释 --- problems/0404.左叶子之和.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/problems/0404.左叶子之和.md b/problems/0404.左叶子之和.md index fb574794..ffcd2c8c 100644 --- a/problems/0404.左叶子之和.md +++ b/problems/0404.左叶子之和.md @@ -42,7 +42,7 @@ if (node->left != NULL && node->left->left == NULL && node->left->right == NULL) ## 递归法 -递归的遍历顺序为后序遍历(左右中),是因为要通过递归函数的返回值来累加求取左叶子数值之和。(前序遍历其实也同样AC) +递归的遍历顺序为后序遍历(左右中),是因为要通过递归函数的返回值来累加求取左叶子数值之和。。 递归三部曲: @@ -230,7 +230,7 @@ class Solution { ## Python -> 递归后序遍历 +**递归后序遍历** ```python3 class Solution: def sumOfLeftLeaves(self, root: TreeNode) -> int: @@ -247,7 +247,7 @@ class Solution: return cur_left_leaf_val + left_left_leaves_sum + right_left_leaves_sum # 中 ``` -> 迭代 +**迭代** ```python3 class Solution: def sumOfLeftLeaves(self, root: TreeNode) -> int: