/** * 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 root; } //前序遍历,先中间节点交换,在进行左右孩子遍历 swap(root); invertTree(root.left); invertTree(root.right); return root; } public void swap (TreeNode root) { TreeNode temp; temp = root.right; root.right = root.left; root.left = temp; } }
标签:right,TreeNode,val,前序,遍历,二叉树,226,root,left From: https://www.cnblogs.com/noedaydayup/p/16757131.html