Leetcode 1379.找出克隆二叉树中的相同节点
1 题目描述(Leetcode题目链接)
给你两棵二叉树,原始树 original 和克隆树 cloned,以及一个位于原始树 original 中的目标节点 target。
其中,克隆树 cloned 是原始树 original 的一个 副本 。
请找出在树 cloned 中,与 target 相同 的节点,并返回对该节点的引用(在 C/C++ 等有指针的语言中返回 节点指针,其他语言返回节点本身)。
注意:
- 你 不能 对两棵二叉树,以及 target 节点进行更改。
- 只能 返回对克隆树 cloned 中已有的节点的引用。
进阶:如果树中允许出现值相同的节点,你将如何解答?
输入: tree = [7,4,3,null,null,6,19], target = 3
输出: 3
解释: 上图画出了树 original 和 cloned。target 节点在树 original 中,用绿色标记。答案是树 cloned 中的黄颜色的节点(其他示例类似)。
提示:
- 树中节点的数量范围为 [1, 10^4] 。
- 同一棵树中,没有值相同的节点。
- target 节点是树 original 中的一个节点,并且不会是 null 。
2 题解
在cloned里面找到与target值相同的节点就可以了,因此只需要遍历树就好了。对于进阶,如果出现相同的值,就需要根据原树判断。
# Definition for a binary tree node.
# 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:
stack = []
while cloned or stack:
while cloned:
stack.append(cloned)
cloned = cloned.left
cloned = stack.pop()
if cloned.val == target.val:
return cloned
cloned = cloned.right