Topic describes
// Invert a binary tree.Copy the code
Answer key
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; *} *} */ / // Beat 100.00% user usage in all Java commits in 0 ms // memory consumption: 36.2 MB, Public TreeNode invertTree(TreeNode root) {if (root == null) return root; public TreeNode invertTree(TreeNode root) {if (root == null) return root; swap(root); invertTree(root.left); invertTree(root.right); return root; } public void swap(TreeNode root) { TreeNode temp = root.left; root.left = root.right; root.right = temp; }}Copy the code