Browse Source

Another one bites the dust

master
Lachlan Jacob 5 years ago
parent
commit
fb92c4a350
3 changed files with 39 additions and 0 deletions
  1. 29
    0
      1379/main.py
  2. 7
    0
      1379/problem.txt
  3. 3
    0
      1379/run.sh

+ 29
- 0
1379/main.py View File

class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None

class Solution:
def getTargetCopy(self, original: TreeNode, cloned: TreeNode, target: TreeNode) -> TreeNode:
return self.findTarget(cloned, target)
def findTarget(self, tree, t):
if tree is not None:
if tree.val == t.val:
return tree
leftFound = self.findTarget(tree.left, t)
rightFound = self.findTarget(tree.right, t)
if leftFound is not None:
return leftFound
elif rightFound is not None:
return rightFound

s = Solution()
o = TreeNode(1)
o.left = TreeNode(3)
c = TreeNode(1)
t = TreeNode(3)
c.left = t
print("Expected: 3")
print("Got:", s.getTargetCopy(o, c, t).val)

+ 7
- 0
1379/problem.txt View File

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.

+ 3
- 0
1379/run.sh View File

#!/bin/bash

python3 main.py

Loading…
Cancel
Save