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;
    }
}
全部评论

相关推荐

04-25 19:29
已编辑
宁波大学 运营
被普调的六边形战士很高大:你我美牛孩
点赞 评论 收藏
分享
程序员牛肉:主要是因为小厂的资金本来就很吃紧,所以更喜欢有实习经历的同学。来了就能上手。 而大厂因为钱多,实习生一天三四百的就不算事。所以愿意培养你,在面试的时候也就不在乎你有没有实习(除非是同级别大厂的实习。) 按照你的简历来看,同质化太严重了。项目也很烂大街。 要么换项目,要么考研。 你现在选择工作的话,前景不是很好了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务