Leetcode 156 Solution
This article provides solution to leetcode question 156 (binary-tree-upside-down)
Access this page by simply typing in "lcs 156" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/binary-tree-upside-down
Solution
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* upsideDownBinaryTree(TreeNode* root) {
if (root == NULL || root->left == NULL)
return root;
auto left_child = root->left;
auto right_child = root->right;
auto left_tree = upsideDownBinaryTree(root->left);
auto right_tree = upsideDownBinaryTree(root->right);
left_child->right = root;
left_child->left = right_tree;
root->left = root->right = NULL;
return left_tree;
}
};