From 44b79f47d53b1ffff80300457f47f820c29252b1 Mon Sep 17 00:00:00 2001 From: Shubham Kondekar Date: Tue, 11 Oct 2022 05:56:11 +0530 Subject: [PATCH 01/21] Different views of binary tree added --- .../binary_tree/diff_views_of_binary_tree.py | 195 ++++++++++++++++++ 1 file changed, 195 insertions(+) create mode 100644 data_structures/binary_tree/diff_views_of_binary_tree.py diff --git a/data_structures/binary_tree/diff_views_of_binary_tree.py b/data_structures/binary_tree/diff_views_of_binary_tree.py new file mode 100644 index 000000000000..58d792553a56 --- /dev/null +++ b/data_structures/binary_tree/diff_views_of_binary_tree.py @@ -0,0 +1,195 @@ +""" +Problem: +Given root of binary Tree, print the + +1. binary-tree-right-side-view +2. binary-tree-left-side-view +3. binary-tree-top-side-view +4. binary-tree-bottom-side-view + + +1. binary-tree-right-side-view + + 3 <- 3 + / \ + 9 20 <- 20 + / \ + 15 7 <- 7 + +Output: [3, 20, 7] + + +2. binary-tree-left-side-view + +3 -> 3 + / \ +9 -> 9 20 + / \ +15 -> 15 7 + +Output: [3, 9, 15] + + +3. binary-tree-top-side-view + + 9 3 20 7 + ⬇ ⬇ ⬇ ⬇ + + 3 + / \ + 9 20 + / \ + 15 7 + +Output: [9, 3, 20, 7] + +4. binary-tree-bottom-side-view + + 3 + / \ + 9 20 + / \ + 15 7 + ↑ ↑ ↑ ↑ + 9 15 20 7 + +Output: [9, 15, 20, 7] + +""" + +from __future__ import annotations + +class TreeNode: + def __init__( + self, val: int = 0, left: TreeNode | None = None, right: TreeNode | None = None + ) -> None: + self.val = val + self.left = left + self.right = right + +def binary_tree_right_side_view(root: TreeNode | None) -> list[int]: + """ + Function returns the right side view of binary tree. + + >>> binary_tree_right_side_view(None) + [] + """ + def dfs(root: TreeNode | None, depth: int, right_view: list[int]) -> None: + if not root: + return + + if depth == len(right_view): + right_view.append(root.val) + + dfs(root.right, depth + 1, right_view) + dfs(root.left, depth + 1, right_view) + + right_view = [] + if not root: + return right_view + dfs(root, 0, right_view) + return right_view + +def binary_tree_left_side_view(root: TreeNode | None) -> list[int]: + """ + Function returns the left side view of binary tree. + + >>> binary_tree_left_side_view(None) + [] + """ + def dfs(root: TreeNode | None, depth: int, left_view: list[int]) -> None: + if not root: + return + + if depth == len(left_view): + left_view.append(root.val) + + dfs(root.left, depth + 1, left_view) + dfs(root.right, depth + 1, left_view) + + left_view = [] + if not root: + return left_view + dfs(root, 0, left_view) + return left_view + +def binary_tree_top_side_view(root: TreeNode | None) -> list[int]: + """ + Function returns the top side view of binary tree. + + >>> binary_tree_top_side_view(None) + [] + """ + from collections import defaultdict + + def bfs(root: TreeNode | None, top_view: list[int]) -> None: + queue = [(root, 0)] + lookup = defaultdict(list) + + while queue: + first = queue.pop(0) + node,hd = first + lookup[hd].append(node.val) + + if node.left: + queue.append((node.left, hd - 1)) + if node.right: + queue.append((node.right, hd + 1)) + + for key, val in sorted(lookup.items(), key = lambda x:x[0]): + top_view.append(val[0]) + + top_view = [] + if not root: + return top_view + bfs(root, top_view) + return top_view + +def binary_tree_bottom_side_view(root: TreeNode | None) -> list[int]: + """ + Function returns the bottom side view of binary tree + + >>> binary_tree_bottom_side_view(None) + [] + """ + from collections import defaultdict + + def bfs(root: TreeNode | None, bottom_view: list[int]) -> None: + queue = [(root, 0)] + lookup = defaultdict(list) + + while queue: + first = queue.pop(0) + node,hd = first + lookup[hd].append(node.val) + + if node.left: + queue.append((node.left, hd - 1)) + if node.right: + queue.append((node.right, hd + 1)) + + for key, val in sorted(lookup.items(), key = lambda x:x[0]): + bottom_view.append(val[-1]) + + bottom_view = [] + if not root: + return bottom_view + bfs(root, bottom_view) + return bottom_view + + +tree_1 = TreeNode(3) +tree_1.left = TreeNode(9) +tree_1.right = TreeNode(20) +tree_1.right.left = TreeNode(15) +tree_1.right.right = TreeNode(7) + +print(binary_tree_right_side_view(tree_1)) # Output: [3, 20, 7] +print(binary_tree_left_side_view(tree_1)) # Output: [3, 9, 15] +print(binary_tree_top_side_view(tree_1)) # Output: [9, 3, 20, 7] +print(binary_tree_bottom_side_view(tree_1)) # Output: [9, 15, 20, 7] + +if __name__ == "__main__": + import doctest + + doctest.testmod() \ No newline at end of file From 70f582d6cd1516e88616487b9f160b19dfaf1ff7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 11 Oct 2022 00:29:18 +0000 Subject: [PATCH 02/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../binary_tree/diff_views_of_binary_tree.py | 43 +++++++++++-------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/data_structures/binary_tree/diff_views_of_binary_tree.py b/data_structures/binary_tree/diff_views_of_binary_tree.py index 58d792553a56..f1757362b54d 100644 --- a/data_structures/binary_tree/diff_views_of_binary_tree.py +++ b/data_structures/binary_tree/diff_views_of_binary_tree.py @@ -59,6 +59,7 @@ from __future__ import annotations + class TreeNode: def __init__( self, val: int = 0, left: TreeNode | None = None, right: TreeNode | None = None @@ -67,20 +68,22 @@ def __init__( self.left = left self.right = right + def binary_tree_right_side_view(root: TreeNode | None) -> list[int]: """ Function returns the right side view of binary tree. - + >>> binary_tree_right_side_view(None) [] """ + def dfs(root: TreeNode | None, depth: int, right_view: list[int]) -> None: if not root: return - + if depth == len(right_view): right_view.append(root.val) - + dfs(root.right, depth + 1, right_view) dfs(root.left, depth + 1, right_view) @@ -90,20 +93,22 @@ def dfs(root: TreeNode | None, depth: int, right_view: list[int]) -> None: dfs(root, 0, right_view) return right_view + def binary_tree_left_side_view(root: TreeNode | None) -> list[int]: """ Function returns the left side view of binary tree. - + >>> binary_tree_left_side_view(None) [] """ + def dfs(root: TreeNode | None, depth: int, left_view: list[int]) -> None: if not root: return - + if depth == len(left_view): left_view.append(root.val) - + dfs(root.left, depth + 1, left_view) dfs(root.right, depth + 1, left_view) @@ -113,6 +118,7 @@ def dfs(root: TreeNode | None, depth: int, left_view: list[int]) -> None: dfs(root, 0, left_view) return left_view + def binary_tree_top_side_view(root: TreeNode | None) -> list[int]: """ Function returns the top side view of binary tree. @@ -128,23 +134,24 @@ def bfs(root: TreeNode | None, top_view: list[int]) -> None: while queue: first = queue.pop(0) - node,hd = first + node, hd = first lookup[hd].append(node.val) - + if node.left: queue.append((node.left, hd - 1)) if node.right: queue.append((node.right, hd + 1)) - for key, val in sorted(lookup.items(), key = lambda x:x[0]): + for key, val in sorted(lookup.items(), key=lambda x: x[0]): top_view.append(val[0]) - + top_view = [] if not root: return top_view bfs(root, top_view) return top_view + def binary_tree_bottom_side_view(root: TreeNode | None) -> list[int]: """ Function returns the bottom side view of binary tree @@ -160,17 +167,17 @@ def bfs(root: TreeNode | None, bottom_view: list[int]) -> None: while queue: first = queue.pop(0) - node,hd = first + node, hd = first lookup[hd].append(node.val) - + if node.left: queue.append((node.left, hd - 1)) if node.right: queue.append((node.right, hd + 1)) - for key, val in sorted(lookup.items(), key = lambda x:x[0]): + for key, val in sorted(lookup.items(), key=lambda x: x[0]): bottom_view.append(val[-1]) - + bottom_view = [] if not root: return bottom_view @@ -185,11 +192,11 @@ def bfs(root: TreeNode | None, bottom_view: list[int]) -> None: tree_1.right.right = TreeNode(7) print(binary_tree_right_side_view(tree_1)) # Output: [3, 20, 7] -print(binary_tree_left_side_view(tree_1)) # Output: [3, 9, 15] -print(binary_tree_top_side_view(tree_1)) # Output: [9, 3, 20, 7] -print(binary_tree_bottom_side_view(tree_1)) # Output: [9, 15, 20, 7] +print(binary_tree_left_side_view(tree_1)) # Output: [3, 9, 15] +print(binary_tree_top_side_view(tree_1)) # Output: [9, 3, 20, 7] +print(binary_tree_bottom_side_view(tree_1)) # Output: [9, 15, 20, 7] if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + doctest.testmod() From 4b443a85816278925090d4fbbbd2700d3f1180a9 Mon Sep 17 00:00:00 2001 From: Shubham Kondekar Date: Tue, 11 Oct 2022 06:05:08 +0530 Subject: [PATCH 03/21] mypy errors resolved --- .../binary_tree/diff_views_of_binary_tree.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/data_structures/binary_tree/diff_views_of_binary_tree.py b/data_structures/binary_tree/diff_views_of_binary_tree.py index f1757362b54d..b065f016e13f 100644 --- a/data_structures/binary_tree/diff_views_of_binary_tree.py +++ b/data_structures/binary_tree/diff_views_of_binary_tree.py @@ -87,7 +87,7 @@ def dfs(root: TreeNode | None, depth: int, right_view: list[int]) -> None: dfs(root.right, depth + 1, right_view) dfs(root.left, depth + 1, right_view) - right_view = [] + right_view: list = [] if not root: return right_view dfs(root, 0, right_view) @@ -112,7 +112,7 @@ def dfs(root: TreeNode | None, depth: int, left_view: list[int]) -> None: dfs(root.left, depth + 1, left_view) dfs(root.right, depth + 1, left_view) - left_view = [] + left_view: list = [] if not root: return left_view dfs(root, 0, left_view) @@ -128,7 +128,7 @@ def binary_tree_top_side_view(root: TreeNode | None) -> list[int]: """ from collections import defaultdict - def bfs(root: TreeNode | None, top_view: list[int]) -> None: + def bfs(root: TreeNode, top_view: list[int]) -> None: queue = [(root, 0)] lookup = defaultdict(list) @@ -145,7 +145,7 @@ def bfs(root: TreeNode | None, top_view: list[int]) -> None: for key, val in sorted(lookup.items(), key=lambda x: x[0]): top_view.append(val[0]) - top_view = [] + top_view: list = [] if not root: return top_view bfs(root, top_view) @@ -161,7 +161,7 @@ def binary_tree_bottom_side_view(root: TreeNode | None) -> list[int]: """ from collections import defaultdict - def bfs(root: TreeNode | None, bottom_view: list[int]) -> None: + def bfs(root: TreeNode, bottom_view: list[int]) -> None: queue = [(root, 0)] lookup = defaultdict(list) @@ -178,7 +178,7 @@ def bfs(root: TreeNode | None, bottom_view: list[int]) -> None: for key, val in sorted(lookup.items(), key=lambda x: x[0]): bottom_view.append(val[-1]) - bottom_view = [] + bottom_view: list = [] if not root: return bottom_view bfs(root, bottom_view) From 7f421cdaedc3e44273184d37d7678935dca5960f Mon Sep 17 00:00:00 2001 From: Shubham Kondekar Date: Tue, 11 Oct 2022 06:14:45 +0530 Subject: [PATCH 04/21] doc test for remaining functions --- .../binary_tree/diff_views_of_binary_tree.py | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/data_structures/binary_tree/diff_views_of_binary_tree.py b/data_structures/binary_tree/diff_views_of_binary_tree.py index b065f016e13f..3f4e639f79b4 100644 --- a/data_structures/binary_tree/diff_views_of_binary_tree.py +++ b/data_structures/binary_tree/diff_views_of_binary_tree.py @@ -78,6 +78,12 @@ def binary_tree_right_side_view(root: TreeNode | None) -> list[int]: """ def dfs(root: TreeNode | None, depth: int, right_view: list[int]) -> None: + """ + A depth first search preorder traversal to append the values at right side of tree. + + >>> dfs([], 0, []) + None + """ if not root: return @@ -103,6 +109,12 @@ def binary_tree_left_side_view(root: TreeNode | None) -> list[int]: """ def dfs(root: TreeNode | None, depth: int, left_view: list[int]) -> None: + """ + A depth first search preorder traversal to append the values at left side of tree. + + >>> dfs([], 0, []) + None + """ if not root: return @@ -129,6 +141,12 @@ def binary_tree_top_side_view(root: TreeNode | None) -> list[int]: from collections import defaultdict def bfs(root: TreeNode, top_view: list[int]) -> None: + """ + A breadth first search traversal with defaultdict ds to append the values of tree from top view + + >>> bfs(TreeNode(5), []) + None + """ queue = [(root, 0)] lookup = defaultdict(list) @@ -142,7 +160,7 @@ def bfs(root: TreeNode, top_view: list[int]) -> None: if node.right: queue.append((node.right, hd + 1)) - for key, val in sorted(lookup.items(), key=lambda x: x[0]): + for key, val in sorted(lookup.items(), key=lambda each: each[0]): top_view.append(val[0]) top_view: list = [] @@ -162,6 +180,12 @@ def binary_tree_bottom_side_view(root: TreeNode | None) -> list[int]: from collections import defaultdict def bfs(root: TreeNode, bottom_view: list[int]) -> None: + """ + A breadth first search traversal with defaultdict ds to append the values of tree from bottom view + + >>> bfs(TreeNode(5), []) + None + """ queue = [(root, 0)] lookup = defaultdict(list) @@ -175,7 +199,7 @@ def bfs(root: TreeNode, bottom_view: list[int]) -> None: if node.right: queue.append((node.right, hd + 1)) - for key, val in sorted(lookup.items(), key=lambda x: x[0]): + for key, val in sorted(lookup.items(), key=lambda each: each[0]): bottom_view.append(val[-1]) bottom_view: list = [] From b1c0cf2d3e0f81c091f755fb98e245733656e164 Mon Sep 17 00:00:00 2001 From: Shubham Kondekar Date: Tue, 11 Oct 2022 06:18:58 +0530 Subject: [PATCH 05/21] Flake8 comments resolved --- .../binary_tree/diff_views_of_binary_tree.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/data_structures/binary_tree/diff_views_of_binary_tree.py b/data_structures/binary_tree/diff_views_of_binary_tree.py index 3f4e639f79b4..7d5ddc35b614 100644 --- a/data_structures/binary_tree/diff_views_of_binary_tree.py +++ b/data_structures/binary_tree/diff_views_of_binary_tree.py @@ -79,7 +79,8 @@ def binary_tree_right_side_view(root: TreeNode | None) -> list[int]: def dfs(root: TreeNode | None, depth: int, right_view: list[int]) -> None: """ - A depth first search preorder traversal to append the values at right side of tree. + A depth first search preorder traversal to append the values at + right side of tree. >>> dfs([], 0, []) None @@ -110,7 +111,8 @@ def binary_tree_left_side_view(root: TreeNode | None) -> list[int]: def dfs(root: TreeNode | None, depth: int, left_view: list[int]) -> None: """ - A depth first search preorder traversal to append the values at left side of tree. + A depth first search preorder traversal to append the values + at left side of tree. >>> dfs([], 0, []) None @@ -142,7 +144,8 @@ def binary_tree_top_side_view(root: TreeNode | None) -> list[int]: def bfs(root: TreeNode, top_view: list[int]) -> None: """ - A breadth first search traversal with defaultdict ds to append the values of tree from top view + A breadth first search traversal with defaultdict ds to append + the values of tree from top view >>> bfs(TreeNode(5), []) None @@ -181,7 +184,8 @@ def binary_tree_bottom_side_view(root: TreeNode | None) -> list[int]: def bfs(root: TreeNode, bottom_view: list[int]) -> None: """ - A breadth first search traversal with defaultdict ds to append the values of tree from bottom view + A breadth first search traversal with defaultdict ds to append + the values of tree from bottom view >>> bfs(TreeNode(5), []) None From 9b20d146e741bc97d311378c768beafd5c5144cb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 11 Oct 2022 00:49:59 +0000 Subject: [PATCH 06/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/binary_tree/diff_views_of_binary_tree.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/data_structures/binary_tree/diff_views_of_binary_tree.py b/data_structures/binary_tree/diff_views_of_binary_tree.py index 7d5ddc35b614..10aa261260ec 100644 --- a/data_structures/binary_tree/diff_views_of_binary_tree.py +++ b/data_structures/binary_tree/diff_views_of_binary_tree.py @@ -79,7 +79,7 @@ def binary_tree_right_side_view(root: TreeNode | None) -> list[int]: def dfs(root: TreeNode | None, depth: int, right_view: list[int]) -> None: """ - A depth first search preorder traversal to append the values at + A depth first search preorder traversal to append the values at right side of tree. >>> dfs([], 0, []) @@ -111,7 +111,7 @@ def binary_tree_left_side_view(root: TreeNode | None) -> list[int]: def dfs(root: TreeNode | None, depth: int, left_view: list[int]) -> None: """ - A depth first search preorder traversal to append the values + A depth first search preorder traversal to append the values at left side of tree. >>> dfs([], 0, []) @@ -144,7 +144,7 @@ def binary_tree_top_side_view(root: TreeNode | None) -> list[int]: def bfs(root: TreeNode, top_view: list[int]) -> None: """ - A breadth first search traversal with defaultdict ds to append + A breadth first search traversal with defaultdict ds to append the values of tree from top view >>> bfs(TreeNode(5), []) @@ -184,7 +184,7 @@ def binary_tree_bottom_side_view(root: TreeNode | None) -> list[int]: def bfs(root: TreeNode, bottom_view: list[int]) -> None: """ - A breadth first search traversal with defaultdict ds to append + A breadth first search traversal with defaultdict ds to append the values of tree from bottom view >>> bfs(TreeNode(5), []) From cff7de041095a7589627f163220fd64a39d5674b Mon Sep 17 00:00:00 2001 From: Shubham Kondekar Date: Tue, 11 Oct 2022 18:51:22 +0530 Subject: [PATCH 07/21] Example moved in if block --- .../binary_tree/diff_views_of_binary_tree.py | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/data_structures/binary_tree/diff_views_of_binary_tree.py b/data_structures/binary_tree/diff_views_of_binary_tree.py index 10aa261260ec..ee4a70d05672 100644 --- a/data_structures/binary_tree/diff_views_of_binary_tree.py +++ b/data_structures/binary_tree/diff_views_of_binary_tree.py @@ -213,18 +213,18 @@ def bfs(root: TreeNode, bottom_view: list[int]) -> None: return bottom_view -tree_1 = TreeNode(3) -tree_1.left = TreeNode(9) -tree_1.right = TreeNode(20) -tree_1.right.left = TreeNode(15) -tree_1.right.right = TreeNode(7) - -print(binary_tree_right_side_view(tree_1)) # Output: [3, 20, 7] -print(binary_tree_left_side_view(tree_1)) # Output: [3, 9, 15] -print(binary_tree_top_side_view(tree_1)) # Output: [9, 3, 20, 7] -print(binary_tree_bottom_side_view(tree_1)) # Output: [9, 15, 20, 7] - if __name__ == "__main__": import doctest + tree_1 = TreeNode(3) + tree_1.left = TreeNode(9) + tree_1.right = TreeNode(20) + tree_1.right.left = TreeNode(15) + tree_1.right.right = TreeNode(7) + + print(binary_tree_right_side_view(tree_1)) # Output: [3, 20, 7] + print(binary_tree_left_side_view(tree_1)) # Output: [3, 9, 15] + print(binary_tree_top_side_view(tree_1)) # Output: [9, 3, 20, 7] + print(binary_tree_bottom_side_view(tree_1)) # Output: [9, 15, 20, 7] + doctest.testmod() From 5fff37688d581bd72acf8b7328ddedcb7e1d0a87 Mon Sep 17 00:00:00 2001 From: Shubham Kondekar Date: Tue, 11 Oct 2022 20:21:20 +0530 Subject: [PATCH 08/21] doctest cases added --- .../binary_tree/diff_views_of_binary_tree.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/data_structures/binary_tree/diff_views_of_binary_tree.py b/data_structures/binary_tree/diff_views_of_binary_tree.py index ee4a70d05672..a064801fb680 100644 --- a/data_structures/binary_tree/diff_views_of_binary_tree.py +++ b/data_structures/binary_tree/diff_views_of_binary_tree.py @@ -75,6 +75,14 @@ def binary_tree_right_side_view(root: TreeNode | None) -> list[int]: >>> binary_tree_right_side_view(None) [] + + >>> tree_1 = TreeNode(3) + >>> tree_1.left = TreeNode(9) + >>> tree_1.right = TreeNode(20) + >>> tree_1.right.left = TreeNode(15) + >>> tree_1.right.right = TreeNode(7) + >>> binary_tree_right_side_view(tree_1) + [3, 20, 7] """ def dfs(root: TreeNode | None, depth: int, right_view: list[int]) -> None: @@ -107,6 +115,15 @@ def binary_tree_left_side_view(root: TreeNode | None) -> list[int]: >>> binary_tree_left_side_view(None) [] + + + >>> tree_1 = TreeNode(3) + >>> tree_1.left = TreeNode(9) + >>> tree_1.right = TreeNode(20) + >>> tree_1.right.left = TreeNode(15) + >>> tree_1.right.right = TreeNode(7) + >>> binary_tree_left_side_view(tree_1) + [3, 9, 15] """ def dfs(root: TreeNode | None, depth: int, left_view: list[int]) -> None: @@ -139,6 +156,15 @@ def binary_tree_top_side_view(root: TreeNode | None) -> list[int]: >>> binary_tree_top_side_view(None) [] + + + >>> tree_1 = TreeNode(3) + >>> tree_1.left = TreeNode(9) + >>> tree_1.right = TreeNode(20) + >>> tree_1.right.left = TreeNode(15) + >>> tree_1.right.right = TreeNode(7) + >>> binary_tree_top_side_view(tree_1) + [9, 3, 20, 7] """ from collections import defaultdict @@ -179,6 +205,14 @@ def binary_tree_bottom_side_view(root: TreeNode | None) -> list[int]: >>> binary_tree_bottom_side_view(None) [] + + >>> tree_1 = TreeNode(3) + >>> tree_1.left = TreeNode(9) + >>> tree_1.right = TreeNode(20) + >>> tree_1.right.left = TreeNode(15) + >>> tree_1.right.right = TreeNode(7) + >>> binary_tree_bottom_side_view(tree_1) + [9, 15, 20, 7] """ from collections import defaultdict From 51d1918d710ab403ff0646e89245aaee41d8caf6 Mon Sep 17 00:00:00 2001 From: Shubham Kondekar Date: Tue, 11 Oct 2022 22:46:10 +0530 Subject: [PATCH 09/21] Cases from if block removed --- .../binary_tree/diff_views_of_binary_tree.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/data_structures/binary_tree/diff_views_of_binary_tree.py b/data_structures/binary_tree/diff_views_of_binary_tree.py index a064801fb680..c343f5137bdc 100644 --- a/data_structures/binary_tree/diff_views_of_binary_tree.py +++ b/data_structures/binary_tree/diff_views_of_binary_tree.py @@ -250,15 +250,4 @@ def bfs(root: TreeNode, bottom_view: list[int]) -> None: if __name__ == "__main__": import doctest - tree_1 = TreeNode(3) - tree_1.left = TreeNode(9) - tree_1.right = TreeNode(20) - tree_1.right.left = TreeNode(15) - tree_1.right.right = TreeNode(7) - - print(binary_tree_right_side_view(tree_1)) # Output: [3, 20, 7] - print(binary_tree_left_side_view(tree_1)) # Output: [3, 9, 15] - print(binary_tree_top_side_view(tree_1)) # Output: [9, 3, 20, 7] - print(binary_tree_bottom_side_view(tree_1)) # Output: [9, 15, 20, 7] - doctest.testmod() From d815e9ed2c16789042e7e5e947251df3e4891a78 Mon Sep 17 00:00:00 2001 From: Shubham Kondekar <40213815+kondekarshubham123@users.noreply.github.com> Date: Mon, 17 Oct 2022 23:25:47 +0530 Subject: [PATCH 10/21] Update data_structures/binary_tree/diff_views_of_binary_tree.py Co-authored-by: Christian Clauss --- data_structures/binary_tree/diff_views_of_binary_tree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/binary_tree/diff_views_of_binary_tree.py b/data_structures/binary_tree/diff_views_of_binary_tree.py index c343f5137bdc..8047db290b9b 100644 --- a/data_structures/binary_tree/diff_views_of_binary_tree.py +++ b/data_structures/binary_tree/diff_views_of_binary_tree.py @@ -168,7 +168,7 @@ def binary_tree_top_side_view(root: TreeNode | None) -> list[int]: """ from collections import defaultdict - def bfs(root: TreeNode, top_view: list[int]) -> None: + def breadth_first_search(root: TreeNode, top_view: list[int]) -> None: """ A breadth first search traversal with defaultdict ds to append the values of tree from top view From dfeb5310e655d3041c9b1911dc2ce0f1b8dd5120 Mon Sep 17 00:00:00 2001 From: Shubham Kondekar <40213815+kondekarshubham123@users.noreply.github.com> Date: Mon, 17 Oct 2022 23:27:10 +0530 Subject: [PATCH 11/21] Update data_structures/binary_tree/diff_views_of_binary_tree.py Co-authored-by: Christian Clauss --- data_structures/binary_tree/diff_views_of_binary_tree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/binary_tree/diff_views_of_binary_tree.py b/data_structures/binary_tree/diff_views_of_binary_tree.py index 8047db290b9b..5534599317c3 100644 --- a/data_structures/binary_tree/diff_views_of_binary_tree.py +++ b/data_structures/binary_tree/diff_views_of_binary_tree.py @@ -216,7 +216,7 @@ def binary_tree_bottom_side_view(root: TreeNode | None) -> list[int]: """ from collections import defaultdict - def bfs(root: TreeNode, bottom_view: list[int]) -> None: + def breadth_first_search(root: TreeNode, bottom_view: list[int]) -> None: """ A breadth first search traversal with defaultdict ds to append the values of tree from bottom view From 9f3e60adbfe7c22e843aceaaac95c372b63b5403 Mon Sep 17 00:00:00 2001 From: Shubham Kondekar Date: Mon, 17 Oct 2022 23:39:31 +0530 Subject: [PATCH 12/21] PR Comments resolved --- .../binary_tree/diff_views_of_binary_tree.py | 82 +++++++------------ 1 file changed, 28 insertions(+), 54 deletions(-) diff --git a/data_structures/binary_tree/diff_views_of_binary_tree.py b/data_structures/binary_tree/diff_views_of_binary_tree.py index 5534599317c3..6c1de2f88bc3 100644 --- a/data_structures/binary_tree/diff_views_of_binary_tree.py +++ b/data_structures/binary_tree/diff_views_of_binary_tree.py @@ -58,15 +58,18 @@ """ from __future__ import annotations +from dataclasses import dataclass +@dataclass class TreeNode: - def __init__( - self, val: int = 0, left: TreeNode | None = None, right: TreeNode | None = None - ) -> None: - self.val = val - self.left = left - self.right = right + val: int + left: TreeNode | None = None + right: TreeNode | None = None + + +def make_tree() -> TreeNode | None: + return TreeNode(3, TreeNode(9), TreeNode(20, TreeNode(15), TreeNode(7))) def binary_tree_right_side_view(root: TreeNode | None) -> list[int]: @@ -76,22 +79,14 @@ def binary_tree_right_side_view(root: TreeNode | None) -> list[int]: >>> binary_tree_right_side_view(None) [] - >>> tree_1 = TreeNode(3) - >>> tree_1.left = TreeNode(9) - >>> tree_1.right = TreeNode(20) - >>> tree_1.right.left = TreeNode(15) - >>> tree_1.right.right = TreeNode(7) - >>> binary_tree_right_side_view(tree_1) + >>> binary_tree_right_side_view(make_tree()) [3, 20, 7] """ - def dfs(root: TreeNode | None, depth: int, right_view: list[int]) -> None: + def depth_first_search(root: TreeNode | None, depth: int, right_view: list[int]) -> None: """ A depth first search preorder traversal to append the values at right side of tree. - - >>> dfs([], 0, []) - None """ if not root: return @@ -99,13 +94,14 @@ def dfs(root: TreeNode | None, depth: int, right_view: list[int]) -> None: if depth == len(right_view): right_view.append(root.val) - dfs(root.right, depth + 1, right_view) - dfs(root.left, depth + 1, right_view) + depth_first_search(root.right, depth + 1, right_view) + depth_first_search(root.left, depth + 1, right_view) right_view: list = [] if not root: return right_view - dfs(root, 0, right_view) + + depth_first_search(root, 0, right_view) return right_view @@ -116,23 +112,14 @@ def binary_tree_left_side_view(root: TreeNode | None) -> list[int]: >>> binary_tree_left_side_view(None) [] - - >>> tree_1 = TreeNode(3) - >>> tree_1.left = TreeNode(9) - >>> tree_1.right = TreeNode(20) - >>> tree_1.right.left = TreeNode(15) - >>> tree_1.right.right = TreeNode(7) - >>> binary_tree_left_side_view(tree_1) + >>> binary_tree_left_side_view(make_tree()) [3, 9, 15] """ - def dfs(root: TreeNode | None, depth: int, left_view: list[int]) -> None: + def depth_first_search(root: TreeNode | None, depth: int, left_view: list[int]) -> None: """ A depth first search preorder traversal to append the values at left side of tree. - - >>> dfs([], 0, []) - None """ if not root: return @@ -140,13 +127,14 @@ def dfs(root: TreeNode | None, depth: int, left_view: list[int]) -> None: if depth == len(left_view): left_view.append(root.val) - dfs(root.left, depth + 1, left_view) - dfs(root.right, depth + 1, left_view) + depth_first_search(root.left, depth + 1, left_view) + depth_first_search(root.right, depth + 1, left_view) left_view: list = [] if not root: return left_view - dfs(root, 0, left_view) + + depth_first_search(root, 0, left_view) return left_view @@ -157,13 +145,7 @@ def binary_tree_top_side_view(root: TreeNode | None) -> list[int]: >>> binary_tree_top_side_view(None) [] - - >>> tree_1 = TreeNode(3) - >>> tree_1.left = TreeNode(9) - >>> tree_1.right = TreeNode(20) - >>> tree_1.right.left = TreeNode(15) - >>> tree_1.right.right = TreeNode(7) - >>> binary_tree_top_side_view(tree_1) + >>> binary_tree_top_side_view(make_tree()) [9, 3, 20, 7] """ from collections import defaultdict @@ -172,9 +154,6 @@ def breadth_first_search(root: TreeNode, top_view: list[int]) -> None: """ A breadth first search traversal with defaultdict ds to append the values of tree from top view - - >>> bfs(TreeNode(5), []) - None """ queue = [(root, 0)] lookup = defaultdict(list) @@ -182,6 +161,7 @@ def breadth_first_search(root: TreeNode, top_view: list[int]) -> None: while queue: first = queue.pop(0) node, hd = first + lookup[hd].append(node.val) if node.left: @@ -195,7 +175,8 @@ def breadth_first_search(root: TreeNode, top_view: list[int]) -> None: top_view: list = [] if not root: return top_view - bfs(root, top_view) + + breadth_first_search(root, top_view) return top_view @@ -206,12 +187,7 @@ def binary_tree_bottom_side_view(root: TreeNode | None) -> list[int]: >>> binary_tree_bottom_side_view(None) [] - >>> tree_1 = TreeNode(3) - >>> tree_1.left = TreeNode(9) - >>> tree_1.right = TreeNode(20) - >>> tree_1.right.left = TreeNode(15) - >>> tree_1.right.right = TreeNode(7) - >>> binary_tree_bottom_side_view(tree_1) + >>> binary_tree_bottom_side_view(make_tree()) [9, 15, 20, 7] """ from collections import defaultdict @@ -220,9 +196,6 @@ def breadth_first_search(root: TreeNode, bottom_view: list[int]) -> None: """ A breadth first search traversal with defaultdict ds to append the values of tree from bottom view - - >>> bfs(TreeNode(5), []) - None """ queue = [(root, 0)] lookup = defaultdict(list) @@ -243,7 +216,8 @@ def breadth_first_search(root: TreeNode, bottom_view: list[int]) -> None: bottom_view: list = [] if not root: return bottom_view - bfs(root, bottom_view) + + breadth_first_search(root, bottom_view) return bottom_view From 70823fdc0baedc4f1b045d82201f14242663f05b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 17 Oct 2022 18:10:46 +0000 Subject: [PATCH 13/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/binary_tree/diff_views_of_binary_tree.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/data_structures/binary_tree/diff_views_of_binary_tree.py b/data_structures/binary_tree/diff_views_of_binary_tree.py index 6c1de2f88bc3..de0878aa92a4 100644 --- a/data_structures/binary_tree/diff_views_of_binary_tree.py +++ b/data_structures/binary_tree/diff_views_of_binary_tree.py @@ -58,6 +58,7 @@ """ from __future__ import annotations + from dataclasses import dataclass @@ -83,7 +84,9 @@ def binary_tree_right_side_view(root: TreeNode | None) -> list[int]: [3, 20, 7] """ - def depth_first_search(root: TreeNode | None, depth: int, right_view: list[int]) -> None: + def depth_first_search( + root: TreeNode | None, depth: int, right_view: list[int] + ) -> None: """ A depth first search preorder traversal to append the values at right side of tree. @@ -116,7 +119,9 @@ def binary_tree_left_side_view(root: TreeNode | None) -> list[int]: [3, 9, 15] """ - def depth_first_search(root: TreeNode | None, depth: int, left_view: list[int]) -> None: + def depth_first_search( + root: TreeNode | None, depth: int, left_view: list[int] + ) -> None: """ A depth first search preorder traversal to append the values at left side of tree. From 588c83b2a9ee918da4eb1d0e12410a93b22f20a3 Mon Sep 17 00:00:00 2001 From: Shubham Kondekar Date: Mon, 17 Oct 2022 23:47:50 +0530 Subject: [PATCH 14/21] flake8 warning resolved --- data_structures/binary_tree/diff_views_of_binary_tree.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/binary_tree/diff_views_of_binary_tree.py b/data_structures/binary_tree/diff_views_of_binary_tree.py index de0878aa92a4..bc08145c6396 100644 --- a/data_structures/binary_tree/diff_views_of_binary_tree.py +++ b/data_structures/binary_tree/diff_views_of_binary_tree.py @@ -174,7 +174,7 @@ def breadth_first_search(root: TreeNode, top_view: list[int]) -> None: if node.right: queue.append((node.right, hd + 1)) - for key, val in sorted(lookup.items(), key=lambda each: each[0]): + for _key, val in sorted(lookup.items(), key=lambda each: each[0]): top_view.append(val[0]) top_view: list = [] @@ -215,7 +215,7 @@ def breadth_first_search(root: TreeNode, bottom_view: list[int]) -> None: if node.right: queue.append((node.right, hd + 1)) - for key, val in sorted(lookup.items(), key=lambda each: each[0]): + for _key, val in sorted(lookup.items(), key=lambda each: each[0]): bottom_view.append(val[-1]) bottom_view: list = [] From 1961c3ef14c8025e5b5b4faafca87dc5027bc96b Mon Sep 17 00:00:00 2001 From: Shubham Kondekar Date: Mon, 17 Oct 2022 23:54:15 +0530 Subject: [PATCH 15/21] Changes revered --- data_structures/binary_tree/diff_views_of_binary_tree.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/binary_tree/diff_views_of_binary_tree.py b/data_structures/binary_tree/diff_views_of_binary_tree.py index bc08145c6396..de0878aa92a4 100644 --- a/data_structures/binary_tree/diff_views_of_binary_tree.py +++ b/data_structures/binary_tree/diff_views_of_binary_tree.py @@ -174,7 +174,7 @@ def breadth_first_search(root: TreeNode, top_view: list[int]) -> None: if node.right: queue.append((node.right, hd + 1)) - for _key, val in sorted(lookup.items(), key=lambda each: each[0]): + for key, val in sorted(lookup.items(), key=lambda each: each[0]): top_view.append(val[0]) top_view: list = [] @@ -215,7 +215,7 @@ def breadth_first_search(root: TreeNode, bottom_view: list[int]) -> None: if node.right: queue.append((node.right, hd + 1)) - for _key, val in sorted(lookup.items(), key=lambda each: each[0]): + for key, val in sorted(lookup.items(), key=lambda each: each[0]): bottom_view.append(val[-1]) bottom_view: list = [] From c26e54e6b26a675ad410c43d5f3d13f2f6cf501c Mon Sep 17 00:00:00 2001 From: Shubham Kondekar Date: Mon, 17 Oct 2022 23:55:56 +0530 Subject: [PATCH 16/21] flake8 issue resolved --- data_structures/binary_tree/diff_views_of_binary_tree.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/data_structures/binary_tree/diff_views_of_binary_tree.py b/data_structures/binary_tree/diff_views_of_binary_tree.py index de0878aa92a4..679546f2e2ba 100644 --- a/data_structures/binary_tree/diff_views_of_binary_tree.py +++ b/data_structures/binary_tree/diff_views_of_binary_tree.py @@ -174,8 +174,8 @@ def breadth_first_search(root: TreeNode, top_view: list[int]) -> None: if node.right: queue.append((node.right, hd + 1)) - for key, val in sorted(lookup.items(), key=lambda each: each[0]): - top_view.append(val[0]) + for pair in sorted(lookup.items(), key=lambda each: each[0]): + top_view.append(pair[1][0]) top_view: list = [] if not root: @@ -215,8 +215,8 @@ def breadth_first_search(root: TreeNode, bottom_view: list[int]) -> None: if node.right: queue.append((node.right, hd + 1)) - for key, val in sorted(lookup.items(), key=lambda each: each[0]): - bottom_view.append(val[-1]) + for pair in sorted(lookup.items(), key=lambda each: each[0]): + bottom_view.append(pair[1][-1]) bottom_view: list = [] if not root: From 75b898bf68f7f2b5844818d626d9705b3b845de7 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 17 Oct 2022 22:17:28 +0200 Subject: [PATCH 17/21] Put the diagrams just above the doctests --- .../binary_tree/diff_views_of_binary_tree.py | 124 +++++++----------- 1 file changed, 49 insertions(+), 75 deletions(-) diff --git a/data_structures/binary_tree/diff_views_of_binary_tree.py b/data_structures/binary_tree/diff_views_of_binary_tree.py index 679546f2e2ba..255c25e7d325 100644 --- a/data_structures/binary_tree/diff_views_of_binary_tree.py +++ b/data_structures/binary_tree/diff_views_of_binary_tree.py @@ -1,64 +1,14 @@ -""" -Problem: -Given root of binary Tree, print the - +r""" +Problem: Given root of a binary tree, return the: 1. binary-tree-right-side-view 2. binary-tree-left-side-view 3. binary-tree-top-side-view 4. binary-tree-bottom-side-view - - -1. binary-tree-right-side-view - - 3 <- 3 - / \ - 9 20 <- 20 - / \ - 15 7 <- 7 - -Output: [3, 20, 7] - - -2. binary-tree-left-side-view - -3 -> 3 - / \ -9 -> 9 20 - / \ -15 -> 15 7 - -Output: [3, 9, 15] - - -3. binary-tree-top-side-view - - 9 3 20 7 - ⬇ ⬇ ⬇ ⬇ - - 3 - / \ - 9 20 - / \ - 15 7 - -Output: [9, 3, 20, 7] - -4. binary-tree-bottom-side-view - - 3 - / \ - 9 20 - / \ - 15 7 - ↑ ↑ ↑ ↑ - 9 15 20 7 - -Output: [9, 15, 20, 7] - """ from __future__ import annotations +from collections import defaultdict from dataclasses import dataclass @@ -69,24 +19,31 @@ class TreeNode: right: TreeNode | None = None -def make_tree() -> TreeNode | None: +def make_tree() -> TreeNode: + """ + >>> make_tree().val + 3 + """ return TreeNode(3, TreeNode(9), TreeNode(20, TreeNode(15), TreeNode(7))) -def binary_tree_right_side_view(root: TreeNode | None) -> list[int]: - """ +def binary_tree_right_side_view(root: TreeNode) -> list[int]: + r""" Function returns the right side view of binary tree. - >>> binary_tree_right_side_view(None) - [] + 3 <- 3 + / \ + 9 20 <- 20 + / \ + 15 7 <- 7 >>> binary_tree_right_side_view(make_tree()) [3, 20, 7] + >>> binary_tree_right_side_view(None) + [] """ - def depth_first_search( - root: TreeNode | None, depth: int, right_view: list[int] - ) -> None: + def depth_first_search(root: TreeNode, depth: int, right_view: list[int]) -> None: """ A depth first search preorder traversal to append the values at right side of tree. @@ -109,19 +66,22 @@ def depth_first_search( def binary_tree_left_side_view(root: TreeNode | None) -> list[int]: - """ + r""" Function returns the left side view of binary tree. - >>> binary_tree_left_side_view(None) - [] + 3 -> 3 + / \ + 9 -> 9 20 + / \ + 15 -> 15 7 >>> binary_tree_left_side_view(make_tree()) [3, 9, 15] + >>> binary_tree_left_side_view(None) + [] """ - def depth_first_search( - root: TreeNode | None, depth: int, left_view: list[int] - ) -> None: + def depth_first_search(root: TreeNode, depth: int, left_view: list[int]) -> None: """ A depth first search preorder traversal to append the values at left side of tree. @@ -143,17 +103,24 @@ def depth_first_search( return left_view -def binary_tree_top_side_view(root: TreeNode | None) -> list[int]: - """ +def binary_tree_top_side_view(root: TreeNode) -> list[int]: + r""" Function returns the top side view of binary tree. - >>> binary_tree_top_side_view(None) - [] + 9 3 20 7 + ⬇ ⬇ ⬇ ⬇ + + 3 + / \ + 9 20 + / \ + 15 7 >>> binary_tree_top_side_view(make_tree()) [9, 3, 20, 7] + >>> binary_tree_top_side_view(None) + [] """ - from collections import defaultdict def breadth_first_search(root: TreeNode, top_view: list[int]) -> None: """ @@ -186,14 +153,21 @@ def breadth_first_search(root: TreeNode, top_view: list[int]) -> None: def binary_tree_bottom_side_view(root: TreeNode | None) -> list[int]: - """ + r""" Function returns the bottom side view of binary tree - >>> binary_tree_bottom_side_view(None) - [] + 3 + / \ + 9 20 + / \ + 15 7 + ↑ ↑ ↑ ↑ + 9 15 20 7 >>> binary_tree_bottom_side_view(make_tree()) [9, 15, 20, 7] + >>> binary_tree_bottom_side_view(None) + [] """ from collections import defaultdict From f46ec011966a65de8324b5c4df2c7c1efb900b5c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 17 Oct 2022 22:22:00 +0200 Subject: [PATCH 18/21] Update diff_views_of_binary_tree.py --- data_structures/binary_tree/diff_views_of_binary_tree.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/data_structures/binary_tree/diff_views_of_binary_tree.py b/data_structures/binary_tree/diff_views_of_binary_tree.py index 255c25e7d325..e3529dbe34e0 100644 --- a/data_structures/binary_tree/diff_views_of_binary_tree.py +++ b/data_structures/binary_tree/diff_views_of_binary_tree.py @@ -15,8 +15,8 @@ @dataclass class TreeNode: val: int - left: TreeNode | None = None - right: TreeNode | None = None + left: TreeNode = None + right: TreeNode = None def make_tree() -> TreeNode: @@ -65,7 +65,7 @@ def depth_first_search(root: TreeNode, depth: int, right_view: list[int]) -> Non return right_view -def binary_tree_left_side_view(root: TreeNode | None) -> list[int]: +def binary_tree_left_side_view(root: TreeNode) -> list[int]: r""" Function returns the left side view of binary tree. @@ -152,7 +152,7 @@ def breadth_first_search(root: TreeNode, top_view: list[int]) -> None: return top_view -def binary_tree_bottom_side_view(root: TreeNode | None) -> list[int]: +def binary_tree_bottom_side_view(root: TreeNode) -> list[int]: r""" Function returns the bottom side view of binary tree From 0cad195cb8604dcc643902e754acd56df9ba82e7 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 17 Oct 2022 22:24:25 +0200 Subject: [PATCH 19/21] Update diff_views_of_binary_tree.py --- data_structures/binary_tree/diff_views_of_binary_tree.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/binary_tree/diff_views_of_binary_tree.py b/data_structures/binary_tree/diff_views_of_binary_tree.py index e3529dbe34e0..2531be6a242c 100644 --- a/data_structures/binary_tree/diff_views_of_binary_tree.py +++ b/data_structures/binary_tree/diff_views_of_binary_tree.py @@ -15,8 +15,8 @@ @dataclass class TreeNode: val: int - left: TreeNode = None - right: TreeNode = None + left: TreeNode | None = None + right: TreeNode | None = None def make_tree() -> TreeNode: From 04ac3eb13b6ac67c28ac5b494255a07c7ee8c264 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 17 Oct 2022 22:27:24 +0200 Subject: [PATCH 20/21] I love mypy --- data_structures/binary_tree/diff_views_of_binary_tree.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/binary_tree/diff_views_of_binary_tree.py b/data_structures/binary_tree/diff_views_of_binary_tree.py index 2531be6a242c..a628528ed77f 100644 --- a/data_structures/binary_tree/diff_views_of_binary_tree.py +++ b/data_structures/binary_tree/diff_views_of_binary_tree.py @@ -43,7 +43,7 @@ def binary_tree_right_side_view(root: TreeNode) -> list[int]: [] """ - def depth_first_search(root: TreeNode, depth: int, right_view: list[int]) -> None: + def depth_first_search(root: TreeNode | None, depth: int, right_view: list[int]) -> None: """ A depth first search preorder traversal to append the values at right side of tree. @@ -81,7 +81,7 @@ def binary_tree_left_side_view(root: TreeNode) -> list[int]: [] """ - def depth_first_search(root: TreeNode, depth: int, left_view: list[int]) -> None: + def depth_first_search(root: TreeNode | None, depth: int, left_view: list[int]) -> None: """ A depth first search preorder traversal to append the values at left side of tree. From afff3f4ffc9a190f755f2bd224a43a5e1beab834 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 17 Oct 2022 20:29:44 +0000 Subject: [PATCH 21/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/binary_tree/diff_views_of_binary_tree.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/data_structures/binary_tree/diff_views_of_binary_tree.py b/data_structures/binary_tree/diff_views_of_binary_tree.py index a628528ed77f..3198d8065918 100644 --- a/data_structures/binary_tree/diff_views_of_binary_tree.py +++ b/data_structures/binary_tree/diff_views_of_binary_tree.py @@ -43,7 +43,9 @@ def binary_tree_right_side_view(root: TreeNode) -> list[int]: [] """ - def depth_first_search(root: TreeNode | None, depth: int, right_view: list[int]) -> None: + def depth_first_search( + root: TreeNode | None, depth: int, right_view: list[int] + ) -> None: """ A depth first search preorder traversal to append the values at right side of tree. @@ -81,7 +83,9 @@ def binary_tree_left_side_view(root: TreeNode) -> list[int]: [] """ - def depth_first_search(root: TreeNode | None, depth: int, left_view: list[int]) -> None: + def depth_first_search( + root: TreeNode | None, depth: int, left_view: list[int] + ) -> None: """ A depth first search preorder traversal to append the values at left side of tree.