From 566136b57d487df7ad20f086450b2f40ec0511cc Mon Sep 17 00:00:00 2001 From: rsimpson2 Date: Sat, 24 Jan 2026 08:07:52 -0500 Subject: [PATCH] Blind 75: Flip Binary Tree --- README.md | 8 +--- greatfrontend/blind-75/README.md | 1 + .../blind-75/trees/flip-binary-tree/README.md | 42 +++++++++++++++++++ .../flip-binary-tree/flip-binary-tree.ts | 23 ++++++++++ 4 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 greatfrontend/blind-75/trees/flip-binary-tree/README.md create mode 100644 greatfrontend/blind-75/trees/flip-binary-tree/flip-binary-tree.ts diff --git a/README.md b/README.md index 8c1a75f..9b7ecdd 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ A comprehensive collection of coding challenges from multiple platforms for lear | Platform | Focus Area | Problems Solved | | ------------------------------- | ---------------------------- | --------------- | | [LeetCode](#leetcode) | Data Structures & Algorithms | 171 | -| [GreatFrontEnd](#greatfrontend) | Frontend Engineering | 13 | +| [GreatFrontEnd](#greatfrontend) | Frontend Engineering | 14 | ## Platforms @@ -23,12 +23,6 @@ Various places where I enjoy practicing solving coding challenges: **Focus**: Data structures, algorithms, and problem-solving patterns -**Progress**: - -- Easy: 80 problems -- Medium: 77 problems -- Hard: 14 problems - **Quick Links**: - [Browse LeetCode Solutions →](./leetcode/) diff --git a/greatfrontend/blind-75/README.md b/greatfrontend/blind-75/README.md index d1d62e0..013572c 100644 --- a/greatfrontend/blind-75/README.md +++ b/greatfrontend/blind-75/README.md @@ -28,6 +28,7 @@ No problems completed yet - [Binary Search Tree Kth Smallest Element](./trees/bst-kth-smallest/) - Medium - [Binary Tree Level Order Traversal](./trees/level-order-traversal/) - Medium +- [Flip Binary Tree](./trees/flip-binary-tree/) - Easy ### Dynamic Programming diff --git a/greatfrontend/blind-75/trees/flip-binary-tree/README.md b/greatfrontend/blind-75/trees/flip-binary-tree/README.md new file mode 100644 index 0000000..d81636d --- /dev/null +++ b/greatfrontend/blind-75/trees/flip-binary-tree/README.md @@ -0,0 +1,42 @@ +# Flip Binary Tree ![Shield](https://img.shields.io/badge/Level-Easy-green) ![Shield](https://img.shields.io/badge/Category-Trees-blue) + +Link to problem: [https://www.greatfrontend.com/interviews/study/blind75/questions/algo/binary-tree-flip](https://www.greatfrontend.com/interviews/study/blind75/questions/algo/binary-tree-flip) + +Given the `root` node of a binary tree, flip the tree by swapping each node's left and right children, then return the root of the flipped tree. + +The binary tree is represented by a collection of `TreeNodes`, where each node has optional `left` and `right` child nodes, which are also `TreeNode`s. + +A TreeNode has the following interface: + +```typescript +interface TreeNode { + val: number; + left: TreeNode | null; + right: TreeNode | null; +} +``` + +## Input + +- `root: TreeNode`: Root node of the tree. Examples display a level-order traversal of the tree + +## Examples + +```plaintext +Input: root = [2,1,3] +Output: [2,3,1] +Explanation: The left and right children are swapped. + +Input: root = [43,null,-55] +Output: [43,-55] +Explanation: The right child becomes the left child after inversion. + +Input: root = [12,8,18,6,null,null,20] +Output: [12,18,8,20,null,null,6] +Explanation: The tree is inverted, with left and right children of each node being swapped. +``` + +## Constraints + +- 1 <= Number of nodes <= 100 +- -100 <= `TreeNode.val` <= 100 diff --git a/greatfrontend/blind-75/trees/flip-binary-tree/flip-binary-tree.ts b/greatfrontend/blind-75/trees/flip-binary-tree/flip-binary-tree.ts new file mode 100644 index 0000000..4df8510 --- /dev/null +++ b/greatfrontend/blind-75/trees/flip-binary-tree/flip-binary-tree.ts @@ -0,0 +1,23 @@ +export interface TreeNode { + val: number; + left: TreeNode | null; + right: TreeNode | null; +} + +/** + * @time O(n) where n is each node in the tree + * @space O(h) where h is the height of the tree + */ +export function flipBinaryTree(root: TreeNode | null): TreeNode | null { + if (root === null) { + return null; + } + + const rightNode = flipBinaryTree(root.right); + const leftNode = flipBinaryTree(root.left); + + root.left = rightNode; + root.right = leftNode; + + return root; +}