Teaching Kids Programming – Iterative Depth First Search Algorithm to Find a Corresponding Node of a Binary Tree in a Clone of That Tree


Teaching Kids Programming: Videos on Data Structures and Algorithms

Given two binary trees original and cloned and given a reference to a node target in the original tree. The cloned tree is a copy of the original tree. Return a reference to the same node in the cloned tree. Note that you are not allowed to change any of the two trees or the target node and the answer must be a reference to a node in the cloned tree.

find-the-node-in-the-cloned-binary-tree Teaching Kids Programming - Iterative Depth First Search Algorithm to Find a Corresponding Node of a Binary Tree in a Clone of That Tree algorithms python teaching kids programming youtube video

Leetcode 1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree

Example 1:
Input: tree = [7,4,3,null,null,6,19], target = 3
Output: 3
Explanation: In all examples the original and cloned trees are shown. The target node is a green node from the original tree. The answer is the yellow node from the cloned tree.

Example 2:
Input: tree = [7], target = 7
Output: 7

Example 3:
Input: tree = [8,null,6,null,5,null,4,null,3,null,2,null,1], target = 4
Output: 4

Constraints:
The number of nodes in the tree is in the range [1, 104].
The values of the nodes of the tree are unique.
target node is a node from the original tree and is not null.

Follow up: Could you solve the problem if repeated values on the tree are allowed?

Iterative Depth First Search Algorithm to Find a Corresponding Node of a Binary Tree in a Clone of That Tree

Recall that we can use a stack data structure (First In First Out) to emulate the process of Recursion. We push the left children to the stack, pop and navigate to the right nodes:

1
2
3
4
5
6
7
8
9
def iterativeInorderDFS(a: TreeNode):
    sa = []        
    while sa or a:
        while a:
            sa.append(a)
            a = a.left
        a = sa.pop()
        print(f"Node {a.val} visited")
        a = a.right
def iterativeInorderDFS(a: TreeNode):
    sa = []        
    while sa or a:
        while a:
            sa.append(a)
            a = a.left
        a = sa.pop()
        print(f"Node {a.val} visited")
        a = a.right

Then we can do this process for two trees at once. We need two stacks, and need to synchronise the movement for two trees i.e. if we go to the left child of the original tree, we need to move to left for the cloned tree as well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution:
    def getTargetCopy(self, a: TreeNode, b: TreeNode, target: TreeNode) -> TreeNode:
        sa, sb = [], []
        
        while sa or a:
            while a:
                sa.append(a)
                a = a.left
                sb.append(b)
                b = b.left
                
            a = sa.pop()
            b = sb.pop()
            if a is target:
                return b
            
            a = a.right
            b = b.right
class Solution:
    def getTargetCopy(self, a: TreeNode, b: TreeNode, target: TreeNode) -> TreeNode:
        sa, sb = [], []
        
        while sa or a:
            while a:
                sa.append(a)
                a = a.left
                sb.append(b)
                b = b.left
                
            a = sa.pop()
            b = sb.pop()
            if a is target:
                return b
            
            a = a.right
            b = b.right

The time/space complexity is O(N) where N is the number of the nodes in the binary tree, and we are using two stacks. Recursion has max depth limits. With this approach, we manage the stack ourselve and hence there will not be a stack-overflow problem. Practically speaking, the iterative approach might be a bit faster than the Recursive DFS implementation. The Recursion has the overhead over function calls.

Find a Corresponding Node of a Binary Tree in a Clone of That Tree

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
825 words
Last Post: Teaching Kids Programming - Find a Corresponding Node of a Binary Tree in a Clone of That Tree via Recursive Depth First Search Algorithm
Next Post: Teaching Kids Programming - Breadth First Search Algorithm to Find a Corresponding Node of a Binary Tree in a Clone of That Tree

The Permanent URL is: Teaching Kids Programming – Iterative Depth First Search Algorithm to Find a Corresponding Node of a Binary Tree in a Clone of That Tree

Leave a Reply