|
| 1 | +--- |
| 2 | +id: binary-tree-upside-down |
| 3 | +title: Binary Tree Upside Down |
| 4 | +sidebar_label: 0156-Binary Tree Upside Down |
| 5 | +tags: |
| 6 | + - Binary Tree |
| 7 | + - Leet code |
| 8 | +description: "Solution to leetocde 156" |
| 9 | +--- |
| 10 | + |
| 11 | +## Problem Description |
| 12 | + |
| 13 | +Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the original right nodes turned into left leaf nodes. Return the new root. |
| 14 | + |
| 15 | +### Example: |
| 16 | + |
| 17 | +``` |
| 18 | +Input: [1,2,3,4,5] |
| 19 | +
|
| 20 | + 1 |
| 21 | + / \ |
| 22 | + 2 3 |
| 23 | + / \ |
| 24 | +4 5 |
| 25 | +
|
| 26 | +Output: return the root of the binary tree [4,5,2,#,#,3,1] |
| 27 | +
|
| 28 | + 4 |
| 29 | + / \ |
| 30 | + 5 2 |
| 31 | + / \ |
| 32 | + 3 1 |
| 33 | +``` |
| 34 | + |
| 35 | +### Algorithm |
| 36 | + |
| 37 | +1. **Base Case**: If the root is `None` or the root has no left child, return the root. |
| 38 | +2. **Recursive Case**: Recursively process the left child of the root. |
| 39 | +3. **Reassign Pointers**: Once you reach the new root, start reassigning the pointers: |
| 40 | + - The left child's left pointer should point to the original root's right child. |
| 41 | + - The left child's right pointer should point to the original root. |
| 42 | +4. **Clear Original Pointers**: Set the original root's left and right pointers to `None`. |
| 43 | +5. **Return the New Root**: Return the new root of the upside-down tree. |
| 44 | + |
| 45 | +### Python |
| 46 | + |
| 47 | +```python |
| 48 | +from typing import Optional |
| 49 | + |
| 50 | +class TreeNode: |
| 51 | + def __init__(self, val=0, left=None, right=None): |
| 52 | + self.val = val |
| 53 | + self.left = left |
| 54 | + self.right = right |
| 55 | + |
| 56 | +class Solution: |
| 57 | + def upsideDownBinaryTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: |
| 58 | + if root is None or root.left is None: |
| 59 | + return root |
| 60 | + new_root = self.upsideDownBinaryTree(root.left) |
| 61 | + root.left.right = root |
| 62 | + root.left.left = root.right |
| 63 | + root.left = None |
| 64 | + root.right = None |
| 65 | + return new_root |
| 66 | +``` |
| 67 | + |
| 68 | +### C++ |
| 69 | + |
| 70 | +```cpp |
| 71 | +#include <iostream> |
| 72 | +using namespace std; |
| 73 | + |
| 74 | +struct TreeNode { |
| 75 | + int val; |
| 76 | + TreeNode *left; |
| 77 | + TreeNode *right; |
| 78 | + TreeNode(int x) : val(x), left(NULL), right(NULL) {} |
| 79 | +}; |
| 80 | + |
| 81 | +class Solution { |
| 82 | +public: |
| 83 | + TreeNode* upsideDownBinaryTree(TreeNode* root) { |
| 84 | + if (root == nullptr || root->left == nullptr) |
| 85 | + return root; |
| 86 | + TreeNode* new_root = upsideDownBinaryTree(root->left); |
| 87 | + root->left->right = root; |
| 88 | + root->left->left = root->right; |
| 89 | + root->left = nullptr; |
| 90 | + root->right = nullptr; |
| 91 | + return new_root; |
| 92 | + } |
| 93 | +}; |
| 94 | +``` |
| 95 | +
|
| 96 | +### Java |
| 97 | +
|
| 98 | +```java |
| 99 | +class TreeNode { |
| 100 | + int val; |
| 101 | + TreeNode left; |
| 102 | + TreeNode right; |
| 103 | + TreeNode(int x) { val = x; } |
| 104 | +} |
| 105 | +
|
| 106 | +public class Solution { |
| 107 | + public TreeNode upsideDownBinaryTree(TreeNode root) { |
| 108 | + if (root == null || root.left == null) return root; |
| 109 | + TreeNode newRoot = upsideDownBinaryTree(root.left); |
| 110 | + root.left.left = root.right; |
| 111 | + root.left.right = root; |
| 112 | + root.left = null; |
| 113 | + root.right = null; |
| 114 | + return newRoot; |
| 115 | + } |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +### JavaScript |
| 120 | + |
| 121 | +```javascript |
| 122 | +function TreeNode(val) { |
| 123 | + this.val = val; |
| 124 | + this.left = this.right = null; |
| 125 | +} |
| 126 | + |
| 127 | +var upsideDownBinaryTree = function (root) { |
| 128 | + if (root === null || root.left === null) return root; |
| 129 | + let newRoot = upsideDownBinaryTree(root.left); |
| 130 | + root.left.left = root.right; |
| 131 | + root.left.right = root; |
| 132 | + root.left = null; |
| 133 | + root.right = null; |
| 134 | + return newRoot; |
| 135 | +}; |
| 136 | +``` |
0 commit comments