Invert a binary tree.

     4
   /   \
  2     7
 / \   / \
1   3 6   9

to

     4
   /   \
  7     2
 / \   / \
9   6 3   1
    public TreeNode invertTree(TreeNode root) {
        return invert(root);
    }
    private TreeNode invert(TreeNode root) {
        if (root == null) {
            return root;
        }
        TreeNode left = invert(root.left);
        TreeNode right = invert(root.right);
        root.left = right;
        root.right = left;
        return root;
    }

results matching ""

    No results matching ""