题目原址
https://leetcode.com/problems/invert-binary-tree/description/
题目描述
Invert a binary tree.
Example:
Input:
4
/ \
2 7
/ \ / \
1 3 6 9
Output:
4
/ \
7 2
/ \ / \
9 6 3 1
给定一个二叉树,交换其左右子树的节点值
AC代码
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root == null)
return null;
TreeNode tn = root.left;
root.left = root.right;
root.right = tn;
invertTree(root.left);
invertTree(root.right);
return root;
}
}