一:题目
二:上码(前序解法)
/*** 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;* }* }*/
class Solution {public TreeNode invertTree(TreeNode root) {if (root == null) return null;swap(root);//交换结点的时候 用的是root 这样才能改变root的结构invertTree(root.left);invertTree(root.right);return root;}public void swap(TreeNode node) {TreeNode temp = null;temp = node.left;node.left = node.right;node.right = temp;}
}