vault backup: 2022-07-05 10:48:41
This commit is contained in:
parent
c91ead3aa0
commit
b95b655944
2
.obsidian/graph.json
vendored
2
.obsidian/graph.json
vendored
|
@ -32,6 +32,6 @@
|
|||
"repelStrength": 10,
|
||||
"linkStrength": 1,
|
||||
"linkDistance": 250,
|
||||
"scale": 0.975280086798608,
|
||||
"scale": 0.8153718570546582,
|
||||
"close": true
|
||||
}
|
|
@ -68,6 +68,7 @@ tag:#leetcode
|
|||
- [[Leetcode Reshape-The-Matrix]]
|
||||
- [[Leetcode Reverse-Linked-List]]
|
||||
- [[Leetcode Search-a-2D-Matrix]]
|
||||
- [[Leetcode Symmetric-Tree]]
|
||||
- [[Leetcode Two-Sum]]
|
||||
- [[Leetcode Valid-Anagram]]
|
||||
- [[Leetcode Valid-Sudoku]]
|
||||
|
@ -115,6 +116,7 @@ tag:#DS tag:#coding_problem -tag:#template_remove_me
|
|||
- [[Leetcode Reshape-The-Matrix]]
|
||||
- [[Leetcode Reverse-Linked-List]]
|
||||
- [[Leetcode Search-a-2D-Matrix]]
|
||||
- [[Leetcode Symmetric-Tree]]
|
||||
- [[Leetcode Two-Sum]]
|
||||
- [[Leetcode Valid-Anagram]]
|
||||
- [[Leetcode Valid-Sudoku]]
|
||||
|
@ -158,6 +160,7 @@ tag:#algorithm tag:#coding_problem -tag:#template_remove_me
|
|||
- [[Leetcode Ransom-Note]]
|
||||
- [[Leetcode Reverse-Linked-List]]
|
||||
- [[Leetcode Search-a-2D-Matrix]]
|
||||
- [[Leetcode Symmetric-Tree]]
|
||||
- [[Leetcode Two-Sum]]
|
||||
- [[Leetcode Valid-Anagram]]
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
tag:#stack
|
||||
```
|
||||
|
||||
- [[Leetcode Implement-Queue-Using-Stacks]]
|
||||
|
||||
|
||||
##### Links:
|
||||
|
|
|
@ -20,6 +20,8 @@ tag:#DFS
|
|||
|
||||
- [[Leetcode Binary-Tree-Postorder-Traversal]]
|
||||
- [[Leetcode Binary-Tree-Preorder-Traversal]]
|
||||
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
|
||||
- [[Leetcode Symmetric-Tree]]
|
||||
|
||||
|
||||
##### Links:
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
tag:#BFS
|
||||
```
|
||||
|
||||
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
|
||||
|
||||
|
||||
##### Links:
|
||||
|
|
|
@ -20,6 +20,8 @@ tag:#DFS
|
|||
|
||||
- [[Leetcode Binary-Tree-Inorder-Traversal]]
|
||||
- [[Leetcode Binary-Tree-Preorder-Traversal]]
|
||||
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
|
||||
- [[Leetcode Symmetric-Tree]]
|
||||
|
||||
|
||||
##### Links:
|
||||
|
|
|
@ -20,6 +20,8 @@ tag:#DFS
|
|||
|
||||
- [[Leetcode Binary-Tree-Inorder-Traversal]]
|
||||
- [[Leetcode Binary-Tree-Postorder-Traversal]]
|
||||
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
|
||||
- [[Leetcode Symmetric-Tree]]
|
||||
|
||||
|
||||
##### Links:
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
tag:#BFS
|
||||
```
|
||||
|
||||
- [[Leetcode Binary-Tree-Level-Order-Traversal]]
|
||||
|
||||
|
||||
##### Links:
|
||||
|
|
|
@ -15,6 +15,8 @@ tag:#linked_list
|
|||
- [[Floyd's Cycle Finding Algorithm]]
|
||||
- [[Leetcode Linked-List-Cycle]]
|
||||
- [[Leetcode Merge-Two-Sorted-Lists]]
|
||||
- [[Leetcode Remove-Duplicates-From-Sorted-List]]
|
||||
- [[Leetcode Reverse-Linked-List]]
|
||||
- [[Two pointers approach]]
|
||||
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@ tag:#linked_list
|
|||
- [[Floyd's Cycle Finding Algorithm]]
|
||||
- [[Leetcode Linked-List-Cycle]]
|
||||
- [[Leetcode Merge-Two-Sorted-Lists]]
|
||||
- [[Leetcode Remove-Duplicates-From-Sorted-List]]
|
||||
- [[Leetcode Remove-Linked-List-Elements]]
|
||||
- [[Two pointers approach]]
|
||||
|
||||
|
|
|
@ -15,9 +15,10 @@
|
|||
|
||||
##### Related topics:
|
||||
```expander
|
||||
tag:#DFS
|
||||
tag:#DFS or tag:#BFS
|
||||
```
|
||||
|
||||
- [[Leetcode Maximum-Depth-Of-Binary-Tree]]
|
||||
|
||||
|
||||
##### Links:
|
||||
|
@ -51,17 +52,25 @@ Given the `root` of a binary tree, _check whether it is a mirror of itself_ (i.e
|
|||
> [!summary]
|
||||
> This is a #DFS #recursion problem
|
||||
|
||||
Method 1, Recursion:
|
||||
Method 1, DFS-like Recursion:
|
||||
- Base Cases:
|
||||
- left and right are nullptr: true
|
||||
- else if left or right is nullptr: false, must be asymmetric
|
||||
- left->val != right->val: false
|
||||
- return check(left->left, right->right) && check(left->right, right->left)
|
||||
|
||||
Method 2, BFS-like Iteration:
|
||||
In the while loop:
|
||||
- Take two nodes from queue, they should be matched.
|
||||
- if both are nullptr, continue.
|
||||
- if one is nullptr, return false.
|
||||
- if val doesn't match, return false.
|
||||
- add left->left and right->right to queue (they will be matched as a pair)
|
||||
- add left->right and right->left to queue (they will be matched as a pair)
|
||||
|
||||
### Solution
|
||||
|
||||
Recursion
|
||||
Recursion, 16ms
|
||||
```cpp
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
|
@ -101,7 +110,72 @@ public:
|
|||
};
|
||||
```
|
||||
|
||||
Iteration
|
||||
Iteration, 8ms
|
||||
```cpp
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* struct TreeNode {
|
||||
* int val;
|
||||
* TreeNode *left;
|
||||
* TreeNode *right;
|
||||
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
|
||||
* right(right) {}
|
||||
* };
|
||||
*/
|
||||
class Solution {
|
||||
bool checkLeaves(TreeNode *l, TreeNode *r) {
|
||||
// Check if the leaves are symmetric.
|
||||
if (!l && !r) {
|
||||
return true;
|
||||
} else if (!l || !r) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public:
|
||||
bool isSymmetric(TreeNode *root) {
|
||||
// BFS-like iteration, using queue.
|
||||
|
||||
// Ensure root has two childs
|
||||
if (!checkLeaves(root->left, root->right)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
queue<TreeNode *> pending;
|
||||
pending.push(root->left);
|
||||
pending.push(root->right);
|
||||
|
||||
TreeNode *l, *r;
|
||||
|
||||
while (!pending.empty()) {
|
||||
l = pending.front();
|
||||
pending.pop();
|
||||
r = pending.front();
|
||||
pending.pop();
|
||||
|
||||
if (l && r) {
|
||||
// Check val of l and r
|
||||
if (l->val != r->val) {
|
||||
return false;
|
||||
}
|
||||
// Chech if the child nodes are symmetric
|
||||
if (!(checkLeaves(l->left, r->right) &&
|
||||
checkLeaves(l->right, r->left))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Add more to queue
|
||||
pending.push(l->left);
|
||||
pending.push(r->right);
|
||||
pending.push(l->right);
|
||||
pending.push(r->left);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
```
|
|
@ -17,6 +17,6 @@
|
|||
## Projects
|
||||
|
||||
### [Notes for CS](Notes%20for%20CS)
|
||||
### [Chroot manager](ChrootMan)
|
||||
### [Chroot manager](ChrootMan.md)
|
||||
|
||||
## Others
|
||||
|
|
Loading…
Reference in a new issue