/** * Created by hzhou on 2015/6/13. * Email: [email protected] */ publicclassInvertBinaryTree{ public TreeNode invertTree(TreeNode root){ if (root == null) { returnnull; }
Queue<TreeNode> queue = new LinkedList<TreeNode>(); queue.add(root);
while (!queue.isEmpty()) { TreeNode node = queue.poll(); TreeNode left = node.left; TreeNode right = node.right; // exchange left with right node.left = right; node.right = left; if (left != null) { queue.add(left); } if (right != null) { queue.add(right); } }