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;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章