-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvertBinaryTree.java
More file actions
50 lines (45 loc) · 1.36 KB
/
InvertBinaryTree.java
File metadata and controls
50 lines (45 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* LeetCode problem 226. Invert Binary Tree: https://leetcode.com/problems/invert-binary-tree/
*/
public class Solution {
/**
* Inverts the given binary tree
* Time Complexity: O(N), where N = the number of nodes in the given tree
* Every node in the tree is visited, and its children are swapped. Thus, the complexity is equal to the number of
* nodes in the tree.
* <p>
* Space Complexity: O(1) or O(H), where H = the height of the given tree.
* The only extra memory used is the one on the recursive stack, which at most is equal to the height of the tree
*
* @param root the root of the binary tree
* @return the inverted binary tree
*/
public TreeNode invertTree(TreeNode root) {
// base case
if (root == null) return null;
TreeNode right = root.right;
TreeNode left = root.left;
// swap the left and right
root.right = left;
root.left = right;
// invert each child
invertTree(root.right);
invertTree(root.left);
return root;
}
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}