前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >47 Find a Corresponding Node of a Binary Tree in a Clone of That Tree

47 Find a Corresponding Node of a Binary Tree in a Clone of That Tree

作者头像
devi
发布2021-08-18 16:19:12
发布2021-08-18 16:19:12
23300
代码可运行
举报
文章被收录于专栏:搬砖记录搬砖记录
运行总次数:0
代码可运行

题目

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:

代码语言:javascript
代码运行次数:0
复制
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.
代码语言:javascript
代码运行次数:0
复制
class Solution {
    public final TreeNode getTargetCopy(final TreeNode original, final TreeNode cloned, final TreeNode target) {
        
    }
}

分析

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

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

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

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

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


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

解答

代码语言:javascript
代码运行次数:0
复制
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);
    }
}

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

代码语言:javascript
代码运行次数:0
复制
/**
 * 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;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/03/12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目
  • 分析
  • 解答
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档