Leetcode 226: Invert Binary Tree

Question: https://leetcode.com/problems/invert-binary-tree/

Solution:

class Solution {
    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return root;
        }
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Press ESC to close