MrainW's Home

All things come to those who wait!

0%

LeetCode 226. Invert Binary Tree

Question

Given the root of a binary tree, invert the tree, and return its root.

https://leetcode.com/problems/invert-binary-tree/

Solution

  • Solution1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public TreeNode invertTree(TreeNode root) {
if(null == root){
return null;
}

TreeNode left = invertTree(root.left);
TreeNode right = invertTree(root.right);
root.left = right;
root.right = left;

return root;
}
}

Complexity:

Time complexity: O(n)

Space complexity: O(h)

Welcome to my other publishing channels