Day14-BinaryTree02

Guoyi Lv3

226. Invert Binary Tree

Node:

Time Complexity && Space Complexity

  • Time Complexity: O(n)
  • Space Complexity: O(n)
1
2
3
4
5
6
7
8
9
var invertTree = function (root) {
if (!root) return null;
let temp = root.right;
root.right = root.left;
root.left = temp;
root.left && invertTree(root.left);
root.right && invertTree(root.right);
return root;
};

101. Symmetric Tree

Node:

Time Complexity && Space Complexity

  • Time Complexity: O(n)
  • Space Complexity: O(n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
var isSymmetric = function (root) {
if (!root) return false;
return isValSame(root.left, root.right);
};

var isValSame = function (left, right) {
if (left && right) {
if (left.val !== right.val) {
return false;
} else {
return (
isValSame(left.left, right.right) && isValSame(left.right, right.left)
);
}
} else if ((!left && right) || (left && !right)) {
return false;
} else {
return true;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var isSymmetric = function (root) {
if (!root) return false;
const queue = [root.left, root.right];
while (queue.length > 0) {
const leftNode = queue.shift();
const rightNode = queue.shift();
if (!leftNode && !rightNode) {
continue;
}
if (!leftNode || !rightNode || leftNode.val !== rightNode.val) {
return false;
}
queue.push(leftNode.left);
queue.push(rightNode.right);
queue.push(leftNode.right);
queue.push(rightNode.left);
}
return true;
};
  • Title: Day14-BinaryTree02
  • Author: Guoyi
  • Created at : 2024-09-18 22:20:47
  • Updated at : 2024-12-07 03:58:41
  • Link: https://guoyiwang.github.io/Algorithm/Day14-BinaryTree02/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments