Recursive C# solution

  • 0
    public TreeNode InvertTree(TreeNode root) {
           if (root == null) return null;
    	if (root.left == null && root.right == null) return root;
    	if (root.left != null)  InvertTree(root.left);
    	if (root.right != null)  InvertTree(root.right);
    	var temp = root.left;
    	root.left = root.right;
    	root.right = temp;
    	return root;

Log in to reply

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