From 4f3afc1f752d16155bb64f78d47d0784a8d757b9 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 5 Aug 2022 09:38:07 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.0623 No.0623.Add One Row to Tree --- .../0623.Add One Row to Tree/README.md | 200 +++++++++++++++++- .../0623.Add One Row to Tree/README_EN.md | 196 ++++++++++++++++- .../0623.Add One Row to Tree/Solution.py | 11 +- 3 files changed, 392 insertions(+), 15 deletions(-) diff --git a/solution/0600-0699/0623.Add One Row to Tree/README.md b/solution/0600-0699/0623.Add One Row to Tree/README.md index 333d19450ced1..16225838e40f2 100644 --- a/solution/0600-0699/0623.Add One Row to Tree/README.md +++ b/solution/0600-0699/0623.Add One Row to Tree/README.md @@ -54,6 +54,10 @@ +**方法一:DFS** + +**方法二:BFS** + ### **Python3** @@ -68,24 +72,50 @@ # self.left = left # self.right = right class Solution: - def addOneRow(self, root: TreeNode, val: int, depth: int) -> TreeNode: + def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]: def dfs(root, d): if root is None: return if d == depth - 1: - l = TreeNode(val=val, left=root.left) - r = TreeNode(val=val, right=root.right) - root.left, root.right = l, r + root.left = TreeNode(val, root.left, None) + root.right = TreeNode(val, None, root.right) return dfs(root.left, d + 1) dfs(root.right, d + 1) if depth == 1: - return TreeNode(val=val, left=root) + return TreeNode(val, root) dfs(root, 1) return root ``` +```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 addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]: + if depth == 1: + return TreeNode(val, root) + q = deque([root]) + i = 0 + while q: + i += 1 + for _ in range(len(q)): + node = q.popleft() + if node.left: + q.append(node.left) + if node.right: + q.append(node.right) + if i == depth - 1: + node.left = TreeNode(val, node.left, None) + node.right = TreeNode(val, None, node.right) + return root +``` + ### **Java** @@ -137,6 +167,51 @@ class Solution { } ``` +```java +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + public TreeNode addOneRow(TreeNode root, int val, int depth) { + if (depth == 1) { + return new TreeNode(val, root, null); + } + Deque q = new ArrayDeque<>(); + q.offer(root); + int i = 0; + while (!q.isEmpty()) { + ++i; + for (int k = q.size(); k > 0; --k) { + TreeNode node = q.pollFirst(); + if (node.left != null) { + q.offer(node.left); + } + if (node.right != null) { + q.offer(node.right); + } + if (i == depth - 1) { + node.left = new TreeNode(val, node.left, null); + node.right = new TreeNode(val, null, node.right); + } + } + } + return root; + } +} +``` + ### **C++** ```cpp @@ -180,6 +255,45 @@ public: }; ``` +```cpp +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + TreeNode* addOneRow(TreeNode* root, int val, int depth) { + if (depth == 1) return new TreeNode(val, root, nullptr); + queue q{{root}}; + int i = 0; + while (!q.empty()) + { + ++i; + for (int k = q.size(); k; --k) + { + TreeNode* node = q.front(); + q.pop(); + if (node->left) q.push(node->left); + if (node->right) q.push(node->right); + if (i == depth - 1) + { + node->left = new TreeNode(val, node->left, nullptr); + node->right = new TreeNode(val, nullptr, node->right); + } + } + } + return root; + } +}; +``` + ### **Go** ```go @@ -213,8 +327,84 @@ func addOneRow(root *TreeNode, val int, depth int) *TreeNode { } ``` +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func addOneRow(root *TreeNode, val int, depth int) *TreeNode { + if depth == 1 { + return &TreeNode{val, root, nil} + } + q := []*TreeNode{root} + i := 0 + for len(q) > 0 { + i++ + for k := len(q); k > 0; k-- { + node := q[0] + q = q[1:] + if node.Left != nil { + q = append(q, node.Left) + } + if node.Right != nil { + q = append(q, node.Right) + } + if i == depth-1 { + node.Left = &TreeNode{val, node.Left, nil} + node.Right = &TreeNode{val, nil, node.Right} + } + } + } + return root +} +``` + ### **TypeScript** +```ts +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ + +function addOneRow( + root: TreeNode | null, + val: number, + depth: number, +): TreeNode | null { + function dfs(root, d) { + if (!root) { + return; + } + if (d == depth - 1) { + root.left = new TreeNode(val, root.left, null); + root.right = new TreeNode(val, null, root.right); + return; + } + dfs(root.left, d + 1); + dfs(root.right, d + 1); + } + if (depth == 1) { + return new TreeNode(val, root); + } + dfs(root, 1); + return root; +} +``` + ```ts /** * Definition for a binary tree node. diff --git a/solution/0600-0699/0623.Add One Row to Tree/README_EN.md b/solution/0600-0699/0623.Add One Row to Tree/README_EN.md index 7947e6ea364da..6908e14f32acf 100644 --- a/solution/0600-0699/0623.Add One Row to Tree/README_EN.md +++ b/solution/0600-0699/0623.Add One Row to Tree/README_EN.md @@ -57,24 +57,50 @@ # self.left = left # self.right = right class Solution: - def addOneRow(self, root: TreeNode, val: int, depth: int) -> TreeNode: + def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]: def dfs(root, d): if root is None: return if d == depth - 1: - l = TreeNode(val=val, left=root.left) - r = TreeNode(val=val, right=root.right) - root.left, root.right = l, r + root.left = TreeNode(val, root.left, None) + root.right = TreeNode(val, None, root.right) return dfs(root.left, d + 1) dfs(root.right, d + 1) if depth == 1: - return TreeNode(val=val, left=root) + return TreeNode(val, root) dfs(root, 1) return root ``` +```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 addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]: + if depth == 1: + return TreeNode(val, root) + q = deque([root]) + i = 0 + while q: + i += 1 + for _ in range(len(q)): + node = q.popleft() + if node.left: + q.append(node.left) + if node.right: + q.append(node.right) + if i == depth - 1: + node.left = TreeNode(val, node.left, None) + node.right = TreeNode(val, None, node.right) + return root +``` + ### **Java** ```java @@ -124,6 +150,51 @@ class Solution { } ``` +```java +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + public TreeNode addOneRow(TreeNode root, int val, int depth) { + if (depth == 1) { + return new TreeNode(val, root, null); + } + Deque q = new ArrayDeque<>(); + q.offer(root); + int i = 0; + while (!q.isEmpty()) { + ++i; + for (int k = q.size(); k > 0; --k) { + TreeNode node = q.pollFirst(); + if (node.left != null) { + q.offer(node.left); + } + if (node.right != null) { + q.offer(node.right); + } + if (i == depth - 1) { + node.left = new TreeNode(val, node.left, null); + node.right = new TreeNode(val, null, node.right); + } + } + } + return root; + } +} +``` + ### **C++** ```cpp @@ -167,6 +238,45 @@ public: }; ``` +```cpp +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + TreeNode* addOneRow(TreeNode* root, int val, int depth) { + if (depth == 1) return new TreeNode(val, root, nullptr); + queue q{{root}}; + int i = 0; + while (!q.empty()) + { + ++i; + for (int k = q.size(); k; --k) + { + TreeNode* node = q.front(); + q.pop(); + if (node->left) q.push(node->left); + if (node->right) q.push(node->right); + if (i == depth - 1) + { + node->left = new TreeNode(val, node->left, nullptr); + node->right = new TreeNode(val, nullptr, node->right); + } + } + } + return root; + } +}; +``` + ### **Go** ```go @@ -200,8 +310,84 @@ func addOneRow(root *TreeNode, val int, depth int) *TreeNode { } ``` +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func addOneRow(root *TreeNode, val int, depth int) *TreeNode { + if depth == 1 { + return &TreeNode{val, root, nil} + } + q := []*TreeNode{root} + i := 0 + for len(q) > 0 { + i++ + for k := len(q); k > 0; k-- { + node := q[0] + q = q[1:] + if node.Left != nil { + q = append(q, node.Left) + } + if node.Right != nil { + q = append(q, node.Right) + } + if i == depth-1 { + node.Left = &TreeNode{val, node.Left, nil} + node.Right = &TreeNode{val, nil, node.Right} + } + } + } + return root +} +``` + ### **TypeScript** +```ts +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ + +function addOneRow( + root: TreeNode | null, + val: number, + depth: number, +): TreeNode | null { + function dfs(root, d) { + if (!root) { + return; + } + if (d == depth - 1) { + root.left = new TreeNode(val, root.left, null); + root.right = new TreeNode(val, null, root.right); + return; + } + dfs(root.left, d + 1); + dfs(root.right, d + 1); + } + if (depth == 1) { + return new TreeNode(val, root); + } + dfs(root, 1); + return root; +} +``` + ```ts /** * Definition for a binary tree node. diff --git a/solution/0600-0699/0623.Add One Row to Tree/Solution.py b/solution/0600-0699/0623.Add One Row to Tree/Solution.py index b86acf0f18700..d5f365c906632 100644 --- a/solution/0600-0699/0623.Add One Row to Tree/Solution.py +++ b/solution/0600-0699/0623.Add One Row to Tree/Solution.py @@ -5,19 +5,20 @@ # self.left = left # self.right = right class Solution: - def addOneRow(self, root: TreeNode, val: int, depth: int) -> TreeNode: + def addOneRow( + self, root: Optional[TreeNode], val: int, depth: int + ) -> Optional[TreeNode]: def dfs(root, d): if root is None: return if d == depth - 1: - l = TreeNode(val=val, left=root.left) - r = TreeNode(val=val, right=root.right) - root.left, root.right = l, r + root.left = TreeNode(val, root.left, None) + root.right = TreeNode(val, None, root.right) return dfs(root.left, d + 1) dfs(root.right, d + 1) if depth == 1: - return TreeNode(val=val, left=root) + return TreeNode(val, root) dfs(root, 1) return root