Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse A Binary Tree (Left to Right) [closed]

I was looking at interview questions and I recently came upon one that asked you how to reverse a general binary tree, like flip it from right to left.

So for example if we had the binary tree

     6
   /   \
  3     4
 / \   / \
7   3 8   1

Reversing it would create

     6
   /   \
  4     3
 / \   / \
1   8 3   7

You can see that the new tree is the mirror image of the original tree.

I haven't been able to think of a good implementation on how to solve this problem. Can anyone offer any good ideas?

like image 702
user1234831 Avatar asked Feb 27 '12 05:02

user1234831


People also ask

What is binary tree data structure?

In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child.

What is a binary tree used for?

In computing, binary trees are mainly used for searching and sorting as they provide a means to store data hierarchically. Some common operations that can be conducted on binary trees include insertion, deletion, and traversal.

What is a balance binary tree?

A balanced binary tree, also referred to as a height-balanced binary tree, is defined as a binary tree in which the height of the left and right subtree of any node differ by not more than 1. To learn more about the height of a tree/node, visit Tree Data Structure.


3 Answers

You can use recursion. We swap the left and right child of a node, in-place, and then do the same for its children:

static void reverseTree(final TreeNode root) {
    final TreeNode temp = root.right;
    root.right = root.left;
    root.left = temp;
    
    if (root.left != null) {
        reverseTree(root.left);
    }
    
    if (root.right != null) {
        reverseTree(root.right);
    }
}

To handle the case where the parameter root may be null:

static void reverseTree(final TreeNode root) {
    if (root == null) {
        return;
    }
    
    final TreeNode temp = root.right;
    root.right = root.left;
    root.left = temp;
    
    reverseTree(root.left);
    
    reverseTree(root.right);
}
like image 165
Petar Ivanov Avatar answered Oct 16 '22 12:10

Petar Ivanov


Reverse a binary tree in O(1) in C/C++.

struct NormalNode {
  int value;
  struct NormalNode *left;
  struct NormalNode *right;
};

struct ReversedNode {
  int value;
  struct ReversedNode *right;
  struct ReversedNode *left;
};

struct ReversedNode *reverseTree(struct NormalNode *root) {
  return (struct ReversedNode *)root;
}
like image 22
Zhipeng YANG Avatar answered Oct 16 '22 13:10

Zhipeng YANG


There are a couple interesting parts to this question. First, since your language is Java, you're most likely to have a generic Node class, something like this:

class Node<T> {
    private final T data;
    private final Node left;
    private final Node right;
    public Node<T>(final T data, final Node left, final Node right) {
        this.data  = data;
        this.left  = left;
        this.right = right;
    }
    ....
}

Secondly, reversing, sometimes called inverting, can be done either by mutating the left and right fields of the node, or by creating a new node just like the original but with its left and right children "reversed." The former approach is shown in another answer, while the second approach is shown here:

class Node<T> {
    // See fields and constructor above...

    public Node<T> reverse() {
        Node<T> newLeftSubtree = right == null ? null : right.reverse();
        Node<T> newRightSubtree = left == null ? null : left.reverse();
        return Node<T>(data, newLeftSubtree, newRightSubtree); 
    }
}

The idea of not mutating a data structure is one of the ideas behind persistent data structures, which are pretty interesting.

like image 5
Ray Toal Avatar answered Oct 16 '22 12:10

Ray Toal