我的LeetCode代码仓:https://github.com/617076674/LeetCode
原题链接:https://leetcode-cn.com/problems/invert-binary-tree/description/
题目描述:
知识点:递归、二叉树
思路:递归地翻转一棵二叉树
时间复杂度是O(n),其中n为树中的节点个数。空间复杂度是O(h),其中h为树的高度。
JAVA代码:
public class Solution {
public TreeNode invertTree(TreeNode root) {
if(null == root){
return root;
}
TreeNode leftChild = invertTree(root.right);
TreeNode rightChild = invertTree(root.left);
root.left = leftChild;
root.right = rightChild;
return root;
}
}
LeetCode解题报告: