-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvert Binary Tree.java
49 lines (41 loc) · 1.35 KB
/
Invert Binary Tree.java
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
// https://leetcode.com/problems/invert-binary-tree/
/**
* Definition for a binary tree node.
* 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;
* }
* }
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
/*
* Time Complexity: O(1) because I'm not using any loops in this solution
*
* Space Complexity: O(n) where n = the number of nodes the input tree has. For every node,
* I'm making a recursive call to flip its children.
*/
// If root is null or it doesn't have any children, return itself
// FYI: By verifying it has children, I'm saving time by not adding another method call in the
// memory stack
if (root == null || (root.left == null && root.right == null)) {
return root;
}
// Create a temporary hold for one of its children
TreeNode holder = root.left;
// Flip children
root.left = root.right;
root.right = holder;
// Flip all descendants
invertTree(root.left);
invertTree(root.right);
return root;
}
}