【Leetcode】Invert Binary Tree

题目链接:https://leetcode.com/problems/invert-binary-tree/

题目:

Invert a binary tree.

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

思路:

递归调整左右指针顺序就好了。

算法:

	public TreeNode invertTree(TreeNode root) {
		TreeNode tmp = null;
		if (root == null) {
			return root;
		}
		tmp = root.left;
		root.left = root.right;
		root.right = tmp;
		invertTree(root.left);
		invertTree(root.right);
		return root;
	}



你可能感兴趣的:(【Leetcode】Invert Binary Tree)