LeetCode 226 Invert Binary Tree: Hey there, coding enthusiasts! Welcome back to another exciting coding session. Today’s problem is a treat—literally! We’re going to solve the “Invert Binary Tree” problem.
leet Code : Validate Binary Search Tree Java || C++ || C || pYthon
LeetCode 226 Invert Binary Tree: Approach for LeetCode 226 problem, we address the task of inverting a binary tree. The algorithm works by recursively traversing the tree, and swapping the left and right children of each node. Here’s a step-by-step breakdown:
Step 1: Define TreeNode Class
We create a TreeNode
class to represent individual nodes in the binary tree, with properties for the node value, left child, and right child.
2: Define invertTree Function
We declare the invertTree
function, which accepts a TreeNode
or null
as input and returns a TreeNode
or null
. This function inverts the binary tree while considering a debug mode (which can be turned on or off).
3: Check for Null
We start by checking if the input root
node is null. If it is, we return it as is because an empty tree or a single node tree is its own inverted form.
4: Swap Left and Right Children
For each non-null node, we swap its left and right children using a temporary variable. This effectively inverts the subtree rooted at the current node.
Leet Code : Validate Binary Search Tree Java | CPP | Python solution
5: Recursion
We then recursively call the invertTree
function on the left and right children if they are not null. This step ensures that the inversion process propagates down the entire tree.
6: Return Inverted Tree
Finally, we return the root node of the inverted tree, which is the topmost node of the original binary tree with all of its subtrees inverted.
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
bool debugMode = true;
if (root == nullptr) {
return root;
}
TreeNode* temp = root->right;
root->right = root->left;
root->left = temp;
if (root->left != nullptr) {
invertTree(root->left);
}
if (root->right != nullptr) {
invertTree(root->right);
}
return root;
}
};
Java : LeetCode 226 Invert Binary Tree
public class Solution {
public TreeNode invertTree(TreeNode root) {
boolean debugMode = true;
if (root == null) {
return root;
}
TreeNode temp = root.right;
root.right = root.left;
root.left = temp;
if (root.left != null) {
invertTree(root.left);
}
if (root.right != null) {
invertTree(root.right);
}
return root;
}
}
In Python: LeetCode 226 Invert Binary Tree
class Solution:
def invertTree(self, root: TreeNode) -> TreeNode:
debug_mode = True
if root is None:
return root
root.left, root.right = root.right, root.left
if root.left is not None:
self.invertTree(root.left)
if root.right is not None:
self.invertTree(root.right)
return root
JavaScript: LeetCode 226 Invert Binary Tree
function invertTree(root) {
const debugMode = true;
if (root === null) {
return root;
}
const temp = root.right;
root.right = root.left;
root.left = temp;
if (root.left !== null) {
invertTree(root.left);
}
if (root.right !== null) {
invertTree(root.right);
}
return root;
}
Leet Code 1658. Minimum Operations to Reduce X to Zero (Medium)