# 1.6 Reverse binary tree upside down

![](https://lh5.googleusercontent.com/niYCO901pPSz3_M-OkzYYFKbgSne-TU2Nsf_86SL0h1RO1xTgelvnPc_1Ad90IKa44J9VQfOupGqpJKRN09AJIPrJpMtQRCAaJvnjJxrrBbaPqeB6sns4_4F_DNHQmYyara9reoe)

Assumption: Given tree is not null

Input: TreeNode root

Output: TreeNode reversed tree, not the original tree

Corner case: if root.left = null, return root

Solution:

1. Look at nodes 1(root), 2, 3, and think about how to reverse these three
2. At current level, we need to:

&#x20;        root.left.left = root.right

&#x20;        root.left.right = root

&#x20;        root.left = null

&#x20;        root.right = null

![https://www.geeksforgeeks.org/flip-binary-tree/](https://lh5.googleusercontent.com/J75gT8jVFJAPjquofPDr80fX6JAH8mYns30KBFWZGHx7GW1SjdREhQukraKWRRJ3ZRi8xzuY1a4EgxEc2wQnPeKquwITp5yZzUrNgzuijojW39tJOKoQkyOt7BF3YYNcKPLQaAyV)

```java
public TreeNode reverseTree(TreeNode root) {
  if (root == null || root.left == null) {
    return root;
  }
  TreeNode newRoot = reverseTree(root.left);
  root.left.left = root.right;
  root.left.right = root;
  root.left = null;
  root.right = null;
  return newRoot;
}
```

>
