Easy Java solution


  • 4
    O
    public class Solution {
        public TreeNode invertTree(TreeNode root) {
        	if (root == null) {
        		return null;
        	}
        	TreeNode left = root.left, right = root.right;
        	if (root.left != null) {
        		left = invertTree(root.left);
        	}
        	if (root.right != null) {
        		right = invertTree(root.right);
        	}
    		root.right = left;
    		root.left = right;
            return root;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.