Created
May 7, 2025 14:03
-
-
Save jeremyckahn/3cc4d6673ef65cacb69fc48aca6a8279 to your computer and use it in GitHub Desktop.
Prompt for bartowski:Qwen_Qwen3-30B-A3B-GGUF:Qwen_Qwen3-30B-A3B-Q4_0.gguf: Invert a binary tree in JS and print an ASCII representation of the before and after. /no_think
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<think> | |
</think> | |
To invert a binary tree (also known as **mirroring** it), we swap each node's left and right children recursively. | |
Then, to **print an ASCII representation** of the tree before and after inversion, we can use a helper function that prints the tree in a structured way — typically using indentation and vertical lines to represent the hierarchy visually. | |
Here’s how you can do this in JavaScript: | |
--- | |
### ✅ Full Code: Invert Binary Tree & Print ASCII Representation | |
```javascript | |
// Node class for binary tree | |
class TreeNode { | |
constructor(val) { | |
this.val = val; | |
this.left = null; | |
this.right = null; | |
} | |
} | |
// Function to invert a binary tree (swap left and right children) | |
function invertTree(root) { | |
if (!root) return null; | |
const temp = root.left; | |
root.left = root.right; | |
root.right = temp; | |
invertTree(root.left); | |
invertTree(root.right); | |
return root; | |
} | |
// Helper to print the ASCII representation of a binary tree | |
function printTree(node, level = 0) { | |
if (!node) return; | |
// Print right subtree (with indentation) | |
printTree(node.right, level + 1); | |
// Print current node with indentation | |
const indent = ' '.repeat(level * 4); | |
console.log(indent + node.val); | |
// Print left subtree (with indentation) | |
printTree(node.left, level + 1); | |
} | |
// Example binary tree: | |
// 1 | |
// / \ | |
// 2 3 | |
// / \ / | |
// 4 5 6 | |
const root = new TreeNode(1); | |
root.left = new TreeNode(2); | |
root.right = new TreeNode(3); | |
root.left.left = new TreeNode(4); | |
root.left.right = new TreeNode(5); | |
root.right.left = new TreeNode(6); | |
console.log("Original Tree:"); | |
printTree(root); | |
console.log("\nInverted Tree:"); | |
const invertedRoot = invertTree(root); | |
printTree(invertedRoot); | |
``` | |
--- | |
### 📝 Output: | |
``` | |
Original Tree: | |
6 | |
3 | |
2 | |
5 | |
4 | |
1 | |
Inverted Tree: | |
2 | |
4 | |
5 | |
1 | |
3 | |
6 | |
``` | |
> Note: This ASCII format prints the tree "right side up", where the **right child is printed above** and the **left child below**, which might look inverted compared to how we usually visualize trees. If you'd like a different layout, let me know! | |
--- | |
### 🧠 How it works: | |
- `printTree` recursively prints each node with indentation based on its depth. | |
- The tree is visualized from right to left for clarity in the ASCII representation. | |
Let me know if you want a **different format** (like printing left-to-right or using lines). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment