镜像二叉树

时间:2015-12-06 12:44:15

标签: java algorithm

我正在尝试找到二叉树的镜像。以下是我到目前为止所做的事情:

import treetoolbox.*;

public class MirrorTree extends BinaryTree<String> {

    public MirrorTree(String key) {
        this(null, key, null);
    }

    public MirrorTree(MirrorTree left, String key, MirrorTree right) {
        this.key = key;
        this.left = left;
        this.right = right;
        root = this;
    }

    public MirrorTree mirrorSymmetricTree() {
        if (root == null) {
            return null;
        }

        final MirrorTree left = (MirrorTree) root.left;
        right = root.right;
        root.left = mirrorSymmetricTree(right);
        root.right = mirrorSymmetricTree(left);
        return (MirrorTree) root;
    }

    public static MirrorTree mirrorSymmetricTree(BinaryTree<String> t) {
        return null;
    }
}

我做错了什么?问题应该在这一部分:

if (root == null) {
    return null;
}

final MirrorTree left = (MirrorTree) root.left;
right = root.right;
root.left = mirrorSymmetricTree(right);
root.right = mirrorSymmetricTree(left);
return (MirrorTree) root;

但我觉得我错过了什么。

3 个答案:

答案 0 :(得分:1)

删除此功能:

BackgroundWorker

将参数添加到此函数以使其递归:

LoadUploadsListAsync

答案 1 :(得分:0)

你的问题在这里:

public static MirrorTree mirrorSymmetricTree(BinaryTree<String> t) {
    return null;
}

你没有用这种方法做任何事情!

答案 2 :(得分:0)

假设您使用BinaryTree<E>类似于this documentation

您可以看到live version of my solution

这就是构建BinaryTree<E>的方式,其中BinaryTree<E>是二叉树节点本身,树中的每个节点都是树本身。这就是BinaryTree<E>的插入方法如何

public void insert(T value)
{
    if (this.value == null)
    {
        this.value = value;
        return;
    }
    else
    {
        if (this.value.compareTo(value) >= 0)
        {
            if (this.left == null)
                this.left = new BinaryTree<T>(value);
            else
                this.left.add(value);
        }
        else
        {
            if (this.right == null)
                this.right = new BinaryTree<T>(value);
            else
                this.right.add(value);
        }
    }
}

这是递归函数的样子

    private void mirrorSymmetricTree(MirrorTreeNode<T> m, BinaryTreeNode<T> n)
    {
        if (n == null) // base case
        {
            return;
        }

        if (n.left != null)
        {
            m.left = new MirrorTreeNode<T>(n.left.value);
            mirrorSymmetricTree(m.left, n.left);
        }

        if (n.right != null)
        {
            m.right = new MirrorTreeNode<T>(n.right.value);
            mirrorSymmetricTree(m.right, n.right);
        }
    }

    public static MirrorTree mirrorSymmetricTree(BinaryTree<T> t)
    {
        if (t == null)
        {
            return null;
        }

        if (t.root != null)
        {
            this.root = new MirrorTreeNode<T>(t.root.value);
            mirrorSymmetricTree(this.root, t.root);
        }

        return this;
    }

您的MirrorTree节点看起来像这样

class MirrorTreeNode<T extends Comparable<T>>
{
    public T value;
    public MirrorTreeNode<T> left;
    public MirrorTreeNode<T> right;

    public MirrorTreeNode<T> (T value)
    {
        this.value = value;
        this.left = null;
        this.right = null;
    }

    ..
}

然后,您可以通过mirrorSymmetricTree

上的BinaryTree来镜像树
BinaryTree<String> t1 = new  BinaryTree<>();
t1.addAll({"D","B","F","A","C","E","G"});

//            D
//        B       F
//      A   C   E   G

t1.printDFS();

// A, B, C, D, E, F, G

MirrorTree<String> t2 = new MirrorTree<>();
t2.mirrorSymmetricTree(t1);

// t2 is a copy of t1 now

t2.printDFS();

// A, B, C, D, E, F, G

注释

  • 为了镜像大小为N的二叉树,您必须访问该树中的每个节点一次,因此镜像树的时间复杂度为O(N)

  • 为了镜像二叉树,您存储的项目必须为Comparable,这意味着可以对它们进行比较,以确定是this.value > input还是this.value < input来确定把它放在树上

  • 为了确保项目为Comparable,您可以手动实施,也可以要求模板类型必须实施Comparable<T>界面,强制T为拥有compareTo功能,可让您比较值\键,就好像它们是数字一样,A.compareTo(B) > 0等同于A > B