1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 class Solution { 11 public TreeNode invertTree(TreeNode root) { 12 // base case 13 if(root == null){ 14 return null; 15 } 16 invertTree(root.left); 17 invertTree(root.right); 18 TreeNode tmp = root.left; 19 root.left = root.right; 20 root.right = tmp; 21 return root; 22 } 23 }时间复杂度 O(n)空间复杂度 O(logn)-O(n)
原文地址:https://www.cnblogs.com/juanqiuxiaoxiao/p/8481746.html
时间: 2024-11-05 17:33:50