/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNode left; * public TreeNode right; * public TreeNode(int x) { val = x; } * } */ public class Solution { public TreeNode InvertTree(TreeNode root) { Travel(ref root); return root; } private void Travel(ref TreeNode current) { if(current == null || current.left == null && current.right == null){ return; } Travel(ref current.left); Travel(ref current.right); Swap(ref current); } private void Swap(ref TreeNode n) { var tmp = n.left; n.left = n.right; n.right = tmp; } }