226. 翻转二叉树
小于 1 分钟
226. 翻转二叉树简单
解题思路:使用递归交换左右子树
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root == null) return root;
TreeNode temp = new TreeNode();
if(root.left != null && root.right != null) {
temp = root.left;
root.left = root.right;
root.right = temp;
invertTree(root.left);
invertTree(root.right);
}else if(root.left == null) {
root.left = root.right;
root.right = null;
invertTree(root.left);
}else if(root.right == null) {
root.right = root.left;
root.left = null;
invertTree(root.right);
}
return root;
}
}
Powered by Waline v2.15.5