-
Notifications
You must be signed in to change notification settings - Fork 0
Blind 75: Flip Binary Tree #212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # Flip Binary Tree   | ||
|
|
||
| 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
Comment on lines
+8
to
+9
|
||
| */ | ||
| 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; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clarify empty-tree handling in the spec.
The README says
root: TreeNodewith at least 1 node, but the implementation acceptsTreeNode | null. Please align the input/constraints (allow 0 nodes) or enforce non-null in code. As per coding guidelines, ...🤖 Prompt for AI Agents