Skip to content

Commit

Permalink
Added Python solution for "623. Add One Row to Tree"
Browse files Browse the repository at this point in the history
  • Loading branch information
chemandante committed Oct 5, 2022
1 parent 7e6129d commit 8e3689c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
63 changes: 63 additions & 0 deletions 06/23/m623.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Solved:
# (M) Add One Row to Tree
# https://leetcode.com/problems/add-one-row-to-tree/
# Helper: Building TreeNode's tree from array

from typing import List, Optional


# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None, array=None):
if array:
self.val = array[0]
self._addNode(array, 0)
else:
self.val = val
self.left = left
self.right = right

def _addNode(self, array: List[int], node_idx: int):
node_idx = 2 * node_idx + 1
if node_idx < len(array) and array[node_idx]:
self.left = TreeNode(array[node_idx])
self.left._addNode(array, node_idx)
else:
self.left = None
node_idx += 1
if node_idx < len(array) and array[node_idx]:
self.right = TreeNode(array[node_idx])
self.right._addNode(array, node_idx)
else:
self.right = None


class Solution:
def _processNode(self, node: TreeNode, val: int, depth: int, currDepth: int):
if depth == currDepth:
Lnode = TreeNode(val, left=node.left)
Rnode = TreeNode(val, right=node.right)
node.left = Lnode
node.right = Rnode
else:
if node.left:
self._processNode(node.left, val, depth, currDepth + 1)
if node.right:
self._processNode(node.right, val, depth, currDepth + 1)

def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
if root:
if depth > 1:
self._processNode(root, val, depth - 1, 1)
return root
else:
node = TreeNode(val, left=root)
return node

return None


x = Solution()
tr = TreeNode(array=[4, 2, None, 3, 1])
xx = x.addOneRow(tr, 1, 3)
print(x)
1 change: 0 additions & 1 deletion 09/87/m987.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Solved:
# (H) Vertical Order Traversal of a Binary Tree
# https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/
# Helper: Building TreeNode's tree from array

from typing import List, Optional

Expand Down
3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Here is the list of solved problems. Language images are clickable and lead to t
| 429 | ![M](img/M.png) [N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal/) | | <a href="https://github.com/chemandante/leetcode/blob/master/04/29/m429.py" target="_blank"><img src="img/py.png"></a> |
| 441 | ![E](img/E.png) [Arranging Coins](https://leetcode.com/problems/arranging-coins/) | | <a href="https://github.com/chemandante/leetcode/blob/master/04/41/m441.swift" target="_blank"><img src="img/sw.png"></a> |
| 462 | ![M](img/M.png) [Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/) | | <a href="https://github.com/chemandante/leetcode/blob/master/04/62/m462.cpp" target="_blank"><img src="img/cpp.png"></a> |
| 623 | ![M](img/M.png) [Add One Row to Tree](https://leetcode.com/problems/add-one-row-to-tree/) | | <a href="https://github.com/chemandante/leetcode/blob/master/06/23/m623.py" target="_blank"><img src="img/py.png"></a> |
| 633 | ![M](img/M.png) [Sum of Square Numbers](https://leetcode.com/problems/sum-of-square-numbers/) | | <a href="https://github.com/chemandante/leetcode/blob/master/06/33/m633.py" target="_blank"><img src="img/py.png"></a> |
| 637 | ![E](img/E.png) [Average of Levels in Binary Tree](https://leetcode.com/problems/average-of-levels-in-binary-tree/) | | <a href="https://github.com/chemandante/leetcode/blob/master/06/37/m0637.c" target="_blank"><img src="img/c.png"></a> |
| 668 | ![H](img/H.png) [Kth Smallest Number in Multiplication Table](https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/) | | <a href="https://github.com/chemandante/leetcode/blob/master/06/68/m668.swift" target="_blank"><img src="img/sw.png"></a> |
Expand Down Expand Up @@ -145,7 +146,7 @@ Here is the list of solved problems. Language images are clickable and lead to t
| Helper | Solution | Lang |
|--------|----------|:----:|
| `Py` implementation of ListNode's `__str__` and `__repr__` | 25.&ensp;Reverse Nodes in k-Group | <a href="https://github.com/chemandante/leetcode/blob/master/00/25/m25.py" target="_blank"><img src="img/py.png"></a> |
| Building TreeNode's tree from array | 623.&ensp;Add One Row to Tree | <a href="https://github.com/chemandante/leetcode/blob/master/06/23/m623.py" target="_blank"><img src="img/py.png"></a> |
| Dynamic array | 637.&ensp;Average of Levels in Binary Tree | <a href="https://github.com/chemandante/leetcode/blob/master/06/37/m0637.c" target="_blank"><img src="img/c.png"></a> |
| `Swift` queue using single-linked list | 695.&ensp;Max Area of Island | <a href="https://github.com/chemandante/leetcode/blob/master/06/95/m695.swift" target="_blank"><img src="img/sw.png"></a> |
| Building TreeNode’s tree from array | 814.&ensp;Binary Tree Pruning | <a href="https://github.com/chemandante/leetcode/blob/master/08/14/m814.swift" target="_blank"><img src="img/sw.png"></a> |
| Building TreeNode's tree from array | 987.&ensp;Vertical Order Traversal of a Binary Tree | <a href="https://github.com/chemandante/leetcode/blob/master/09/87/m987.py" target="_blank"><img src="img/py.png"></a> |

0 comments on commit 8e3689c

Please sign in to comment.