47 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) {
        
    }
}

分析

题意:给定两棵树A和B,B是A的复制,给出A上的一个节点,返回B上对应节点。

首先想到的就是值的对比,但是由于可能出现相同值,因此不可行。
这道题,应该就是考如何快速定位节点。

然而,除了“哈弗曼编码”的方式,我没有想到如何确定一个唯一确定的二叉树节点。

哈夫曼编码如何做,请自行百度,这里直接写算法:

先找出原树的哈夫曼编码,然后在克隆树上得到该哈夫曼编码对应的节点。


看了下答案,发现特别简单。。。
只需要同步把左右递归下去,然后与target对比就行了。。。

解答

class Solution {
    public final TreeNode getTargetCopy(final TreeNode original, final TreeNode cloned, final TreeNode target) {
        if (original == null) {
            return null;
        }
        if (original == target) {
            return cloned;
        }
        TreeNode left = getTargetCopy(original.left, cloned.left, target);
        if (left != null) {
            return left;
        }
        return getTargetCopy(original.right, cloned.right, target);
    }
}

这个是复杂的想法,而且报错了。
原因不知道啊。
我觉得这个想法挺好。。。,保留一下错误解答。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

class Solution {
    public final TreeNode getTargetCopy(final TreeNode original, final TreeNode cloned, final TreeNode target) {
        // 根不存在左右,设为-1
        huff(original,target,-1);
        return helper(cloned);
    }
    
    StringBuilder code = new StringBuilder();
    
    // 获得哈夫曼编码,令direction=1时为左,0为右
    void huff(TreeNode root,TreeNode target,int direction){
        if(root==null || root==target) return;
        code.append(direction);
        huff(root.left,target,1);
        huff(root.right,target,0);
    }
    
    // 在克隆树上按哈夫曼编码进行深度遍历
    TreeNode helper(TreeNode root){
        TreeNode tmp=root;
        for(char c:code.toString().toCharArray()){
            tmp=search(tmp,(int)c);
        }
        return tmp;
    }
    
    // 查找子树
    TreeNode search(TreeNode root,int direction){
        if(root!=null)
            return direction==1?root.left:root.right;
        return null;
    }
}
发布了118 篇原创文章 · 获赞 26 · 访问量 8007

猜你喜欢

转载自blog.csdn.net/weixin_43367550/article/details/104821613