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;
}
}
Comments
Post a Comment
Share your thoughts here...