LeetCode 623. Add One Row to Tree — Explained Python3 Solution
Problem Description
Given the root of a binary tree, then the value v
and depth d
, you need to add a row of nodes with value v
at the given depth d
. The root node is at depth 1.
The adding rule is: given a positive integer depth d
, for each NOT null tree nodes N
of depth d-1
, create two tree nodes with value v
as N's
left subtree root and right subtree root. And N's
original left subtree should be the left subtree of the new left subtree root, its original right subtree should be the right subtree of the new right subtree root. If depth d
is 1 that means there is no depth d-1 at all, then create a tree node with value v as the new root of the whole original tree, and the original tree is the new root's left subtree.
Example 1:
Input:
A binary tree as following:
4
/ \
2 6
/ \ /
3 1 5
v = 1
d = 2
Output:
4
/ \
1 1
/ \
2 6
/ \ /
3 1 5
Example 2:
Input:
A binary tree as following:
4
/ \
2 6
/ \ /
3 1 5
v = 1
d = 1
Output:
1
/
4
/ \
2 6
/ \ /
3 1 5
Solution
The key to this problem is that I need to:
1, know what’s the current depth for a node I visit now.
2, when current depth == d-1, I need to create 2 new children nodes and ask the left child’s left pointing to the current node’s left and the right child’s right pointing to the current node's right.
One corner case is what if d ==1. The problem description mentions that during an interview one may be expected to ask in the first place. So it may be a good practice to not look at the examples after reading the problem description; instead, think about the possible examples (to cover as many as possible scenarios) and then read further on provided examples to compare and reflect.
With the above explanation, hopefully below 2 solutions make more sense.
Recursive (DFS) Solution
#recursive solution | |
# https://medium.com/tech-life-fun/leetcode-623-add-one-row-to-tree-explained-python3-solution-8ef2420d27b0?sk=2c6d85c0b8c80813ca195340c7d807cc | |
class Solution: | |
def addOneRow(self, root: TreeNode, v: int, d: int) -> TreeNode: | |
if d == 1: | |
return TreeNode(v, left=root) | |
self.addOneRowHelper(root, v, d, 1) | |
return root | |
def addOneRowHelper(self, root, v, d, level): | |
if d-1 == level: | |
leftTree = TreeNode(v, left=root.left) | |
rightTree = TreeNode(v, right=root.right) | |
root.left = leftTree | |
root.right = rightTree | |
return | |
if root.left: | |
self.addOneRowHelper(root.left, v, d, level+1) | |
if root.right: | |
self.addOneRowHelper(root.right, v, d, level+1) |
Non-recursive(BFS) Solution
# Non-recursive solution (BFS) | |
# https://medium.com/tech-life-fun/leetcode-623-add-one-row-to-tree-explained-python3-solution-8ef2420d27b0?sk=2c6d85c0b8c80813ca195340c7d807cc | |
# 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: TreeNode, v: int, d: int) -> TreeNode: | |
if d == 1: | |
return TreeNode(v, root) | |
lastLayerNodes = [root] | |
curDep = 1 | |
while(lastLayerNodes): | |
for i in range(len(lastLayerNodes)): | |
parentNode = lastLayerNodes.pop(0) | |
if curDep == d - 1: | |
newNode = TreeNode(v, parentNode.left) | |
parentNode.left = newNode | |
newNode = TreeNode(v, None, parentNode.right) | |
parentNode.right = newNode | |
else: | |
if parentNode.left: | |
lastLayerNodes.append(parentNode.left) | |
if parentNode.right: | |
lastLayerNodes.append(parentNode.right) | |
curDep += 1 | |
return root |
Time & Space Complexity
For both above solutions, assuming if there are N nodes, in general, the nodes I need to visit are directly related to N. Therefore the time complexity is also O(N).
When it comes to space complexity:
- The recursive solution requires O(1) space if the function calling stack is not calculated; otherwise, it’s O(N) since the more nodes you have, on average, the deeper you need to go.
- The non-recursive solution needs to store one layer of nodes so it is O(x) where x is the maximum possible nodes in a layer of the tree.
Extended Reading
Python3 cheatsheetAlgorithm Cheatsheet (continuously updated)
Comments
Post a Comment