DSA Practice: How to invert a binary tree

Problem link: https://leetcode.com/problems/invert-binary-tree/

Question type: Easy

Code

class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return root;
}
TreeNode left = root.left;
TreeNode right = root.right;
// inversion logic
root.left = right;
root.right = left;
// process the sub trees
invertTree(left);
invertTree(right);
return root;
}
}

Test Results



Comments

Popular posts from this blog

What is a REST controller & Write a simple REST controller in Spring boot 3.xx

Senior Engineer Interview Prep Notes