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

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.

Follow up: Solve the problem if repeated values on the tree are allowed.

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

Example 4:

Input: tree = [1,2,3,4,5,6,7,8,9,10], target = 5
Output: 5

Example 5:

Input: tree = [1,2,null,3], target = 2
Output: 2

Constraints:

  • The number of nodes in the tree is in the range [1, 10^4].
  • The values of the nodes of the tree are unique.
  • target node is a node from the original tree and is not null.
class Solution {
    public final TreeNode getTargetCopy(final TreeNode original, final TreeNode cloned, final TreeNode target) {
            if (original == null || original == target)
                return cloned;
            TreeNode res = getTargetCopy(original.left, cloned.left, target);
            if (res != null)
                return res;
            return getTargetCopy(original.right, cloned.right, target);
        }
}

原文地址:https://www.cnblogs.com/wentiliangkaihua/p/12702792.html

时间: 2024-10-10 09:12:34

1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree的相关文章

[leetcode]1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree

[leetcode]1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree 链接 leetcode 描述 ??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 tr

lintcode 容易题:Insert Node in a Binary Search Tree 在二叉查找树中插入节点

题目:  在二叉查找树中插入节点 给定一棵二叉查找树和一个新的树节点,将节点插入到树中. 你需要保证该树仍然是一棵二叉查找树.  样例 给出如下一棵二叉查找树,在插入节点6之后这棵二叉查找树可以是这样的: 挑战 能否不使用递归? 解题: 递归的方法比较简单 Java程序: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public

[Lintcode] Insert Node in a Binary Search Tree

Insert Node in a Binary Search Tree Given a binary search tree and a new tree node, insert the node into the tree. You should keep the tree still be a valid binary search tree. Example Given binary search tree as follow, after Insert node 6, the tree

[lintcode easy]Insert Node in a Binary Search Tree

Insert Node in a Binary Search Tree Example Given binary search tree as follow, after Insert node 6, the tree should be: 2 2 / \ / 1 4 --> 1 4 / / \ 3 3 6 Challenge Can you do it without recursion? /** * Definition of TreeNode: * public class TreeNod

Insert Node in a Binary Search Tree

Given a binary search tree and a new tree node, insert the node into the tree. You should keep the tree still be a valid binary search tree. Example Given binary search tree as follow: / 4 / after Insert node 6, the tree should be: / 4 / \ 6 Challeng

LeetCode 671. 二叉树中第二小的节点(Second Minimum Node In a Binary Tree) 9

671. 二叉树中第二小的节点 671. Second Minimum Node In a Binary Tree 题目描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的一个二叉树,你需要输出所有节点中的第二小的值.如果第二小的值不存在的话,输出 -1. 每日一算法 2019/5/12Day 9 LeetCode671. Second Minimum Node In a B

[LeetCode] Second Minimum Node In a Binary Tree

Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes.

671. Second Minimum Node In a Binary Tree 二叉树中第二小节点

Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly twoor zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes. G

[leetcode-671-Second Minimum Node In a Binary Tree]

Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes.