leecode 226. 翻转二叉树,简单

利用递归重复翻转即可,先翻转左子树再翻转又子树都可:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
private TreeNode x;
    public TreeNode invertTree(TreeNode root) {
        if (x==null)
            x = root;
        if (root == null) {
            return x;
        }
        TreeNode a = root.left;
        root.left = root.right;
        root.right = a;
        invertTree(root.left);
        invertTree(root.right);
        return x;
    }
原文地址:https://www.cnblogs.com/junlancer/p/12892964.html