diff --git a/.obsidian/graph.json b/.obsidian/graph.json index 764f226..84cfcd6 100644 --- a/.obsidian/graph.json +++ b/.obsidian/graph.json @@ -32,6 +32,6 @@ "repelStrength": 10, "linkStrength": 1, "linkDistance": 250, - "scale": 0.975280086798608, + "scale": 0.8153718570546582, "close": true } \ No newline at end of file diff --git a/CS notes/CS-index.md b/CS notes/CS-index.md index 6da4a76..844f0ce 100644 --- a/CS notes/CS-index.md +++ b/CS notes/CS-index.md @@ -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]] diff --git a/CS notes/pages/Leetcode Valid-Parentheses.md b/CS notes/pages/Leetcode Valid-Parentheses.md index 10dc50e..572907f 100644 --- a/CS notes/pages/Leetcode Valid-Parentheses.md +++ b/CS notes/pages/Leetcode Valid-Parentheses.md @@ -16,6 +16,7 @@ tag:#stack ``` +- [[Leetcode Implement-Queue-Using-Stacks]] ##### Links: diff --git a/CS notes/pages/Leetcode Binary-Tree-Inorder-Traversal.md b/CS notes/pages/Leetcode Binary-Tree-Inorder-Traversal.md index 5b8a295..3b53a6d 100644 --- a/CS notes/pages/Leetcode Binary-Tree-Inorder-Traversal.md +++ b/CS notes/pages/Leetcode Binary-Tree-Inorder-Traversal.md @@ -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: diff --git a/CS notes/pages/Leetcode Binary-Tree-Level-Order-Traversal.md b/CS notes/pages/Leetcode Binary-Tree-Level-Order-Traversal.md index 9091656..bd11bf1 100644 --- a/CS notes/pages/Leetcode Binary-Tree-Level-Order-Traversal.md +++ b/CS notes/pages/Leetcode Binary-Tree-Level-Order-Traversal.md @@ -18,6 +18,7 @@ tag:#BFS ``` +- [[Leetcode Maximum-Depth-Of-Binary-Tree]] ##### Links: diff --git a/CS notes/pages/Leetcode Binary-Tree-Postorder-Traversal.md b/CS notes/pages/Leetcode Binary-Tree-Postorder-Traversal.md index f2efcd6..ffd4ed2 100644 --- a/CS notes/pages/Leetcode Binary-Tree-Postorder-Traversal.md +++ b/CS notes/pages/Leetcode Binary-Tree-Postorder-Traversal.md @@ -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: diff --git a/CS notes/pages/Leetcode Binary-Tree-Preorder-Traversal.md b/CS notes/pages/Leetcode Binary-Tree-Preorder-Traversal.md index 2d43fff..fd1bec3 100644 --- a/CS notes/pages/Leetcode Binary-Tree-Preorder-Traversal.md +++ b/CS notes/pages/Leetcode Binary-Tree-Preorder-Traversal.md @@ -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: diff --git a/CS notes/pages/Leetcode Maximum-Depth-Of-Binary-Tree.md b/CS notes/pages/Leetcode Maximum-Depth-Of-Binary-Tree.md index 9b56d80..2246e2b 100644 --- a/CS notes/pages/Leetcode Maximum-Depth-Of-Binary-Tree.md +++ b/CS notes/pages/Leetcode Maximum-Depth-Of-Binary-Tree.md @@ -18,6 +18,7 @@ tag:#BFS ``` +- [[Leetcode Binary-Tree-Level-Order-Traversal]] ##### Links: diff --git a/CS notes/pages/Leetcode Remove-Linked-List-Elements.md b/CS notes/pages/Leetcode Remove-Linked-List-Elements.md index c0e2290..d51f1c8 100644 --- a/CS notes/pages/Leetcode Remove-Linked-List-Elements.md +++ b/CS notes/pages/Leetcode Remove-Linked-List-Elements.md @@ -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]] diff --git a/CS notes/pages/Leetcode Reverse-Linked-List.md b/CS notes/pages/Leetcode Reverse-Linked-List.md index a2e26e1..4bd9d75 100644 --- a/CS notes/pages/Leetcode Reverse-Linked-List.md +++ b/CS notes/pages/Leetcode Reverse-Linked-List.md @@ -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]] diff --git a/CS notes/pages/Leetcode Symmetric-Tree.md b/CS notes/pages/Leetcode Symmetric-Tree.md index 761e646..76dc9c4 100644 --- a/CS notes/pages/Leetcode Symmetric-Tree.md +++ b/CS notes/pages/Leetcode Symmetric-Tree.md @@ -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 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; + } +}; ``` \ No newline at end of file diff --git a/ChrootMan.md b/Wiki/ChrootMan.md similarity index 100% rename from ChrootMan.md rename to Wiki/ChrootMan.md diff --git a/Wiki/Wiki-Index.md b/Wiki/Wiki-Index.md index 59e6329..e9ee429 100644 --- a/Wiki/Wiki-Index.md +++ b/Wiki/Wiki-Index.md @@ -17,6 +17,6 @@ ## Projects ### [Notes for CS](Notes%20for%20CS) -### [Chroot manager](ChrootMan) +### [Chroot manager](ChrootMan.md) ## Others