/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} root * @return {TreeNode} */ var invertTree = function(root) { if (root === null) returnnull; var right = arguments.callee(root.right); var left = arguments.callee(root.left); root.left = right; root.right = left; return root; };
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNode left; * public TreeNode right; * public TreeNode(int x) { val = x; } * } */ publicclassSolution { public TreeNode InvertTree(TreeNode root) { if (root == null) returnnull; TreeNode left = InvertTree(root.left); TreeNode right = InvertTree(root.right); root.left = right; root.right = left; return root; } }