简体   繁体   中英

Something wrong with my Invert tree or BFS traverse?

I had already attached two tree to test, but the answer below is weird. Input are two trees and to check if they are inverted version. So i wrote a invert function and a BFS traverse to store the tree node into a String, through comparing the two string to check if they are inverted version.

The first tree is 5 15 14 9 6 3 1 The second tree is: 5 15 14 9 6 3 1 5 14 15 3 6 9 1 No, not mirror imagines

Process finished with exit code 0

public class CheckMirrorTree{

public static void main(String[] args) {

    // add the first tree
    TreeNode root = new TreeNode(5);
    root.left = new TreeNode(15);
    root.right = new TreeNode(14);
    TreeNode rightChild = root.right;
    TreeNode leftChild = root.left;
    rightChild.left = new TreeNode(3);
    rightChild.left.right = new TreeNode(1);
    leftChild.right = new TreeNode(6);
    leftChild.left = new TreeNode(9);
    System.out.println("The first tree is");
    String root1 = traverse(root);

    // add the second tree
    System.out.println("The second tree is:");
    TreeNode secondRoot = new TreeNode(5);
    secondRoot.right = new TreeNode(15);
    secondRoot.left = new TreeNode(14);
    TreeNode secondRightChild = secondRoot.right;
    TreeNode secondLeftChild = secondRoot.left;
    secondRightChild.left = new TreeNode(6);
    secondRightChild.right = new TreeNode(9);
    secondLeftChild.right = new TreeNode(3);
    secondLeftChild.right.left = new TreeNode(1);
    String root2 = traverse(secondRoot);

    if (root1 == root2){
        System.out.println("Yes, mirror images");
    }else {
        System.out.println("No, not mirror imagines");
    }
}


static Deque<TreeNode> queue = new ArrayDeque();
static List<Integer> result = new LinkedList();
static Deque<TreeNode> invertQueue = new ArrayDeque();

static private TreeNode invertTree(TreeNode root) {
    invertQueue.add(root);
    TreeNode reserve = root;
    while (!invertQueue.isEmpty()) {
        if (root.left != null || root.right != null) {
            invertQueue.poll();
            TreeNode temp = root.right;
            root.right = root.left;
            root.left = temp;
            if (root.left != null)
                invertQueue.add(root.left);
            if (root.right != null)
                invertQueue.add(root.right);
        }
        root = invertQueue.poll();
    }

    return reserve;
}


static private String traverse (TreeNode root){ // Each child of a tree is a root of its subtree.
    queue.add(root);
    helper(root);
    // print out the tree
    String res = "";
    for (int i : result) {
        System.out.println(i);
        res += i;
    }
    return res;
}

static private void helper(TreeNode root) {

    while (!queue.isEmpty()) {
        if(root.left != null || root.right != null) {
            TreeNode temp = queue.poll();
            result.add(temp.val);
            if (root.left != null)
                queue.add(root.left);
            if (root.right != null)
                queue.add(root.right);
            helper(queue.peek());
        }
        // deal with the ending nodes
        if (root.left == null && root.right == null) {
            if (queue.size() == 0) {
                result.add(root.val);
                return;
            }
            result.add(queue.poll().val);
            root = queue.peek();
        }
    }
}

}

root1 == root2

is not how you check if two strings are equal. Try:

root1.equals(root2);

Then your if statement should be entered and something will be printed. Since nothing else is getting printed I would suggest posting the traverse method since that's probably where the program is exiting early.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM