From f62d200d651e3cbe6514c907d89b11a0d950744d Mon Sep 17 00:00:00 2001 From: Monique Andrade Date: Thu, 20 Jun 2024 16:29:47 -0700 Subject: [PATCH 1/4] Added the solution for LEetCode problem 144 - Binary Tree Preorder Traversal for C++. There was no solution for C++ for this specific problem. --- .vscode/c_cpp_properties.json | 18 ++++++++ .vscode/launch.json | 7 +++ .vscode/settings.json | 59 ++++++++++++++++++++++++++ .vscode/tasks.json | 29 +++++++++++++ C++/Binary-Tree-Preorder-Traversal.cpp | 27 ++++++++++++ 5 files changed, 140 insertions(+) create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json create mode 100644 .vscode/tasks.json create mode 100644 C++/Binary-Tree-Preorder-Traversal.cpp diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 00000000..980fd575 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "macos-clang-arm64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/clang", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "macos-clang-arm64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..5c7247b4 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,7 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..b9c6ac87 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "clang", + "C_Cpp_Runner.cppCompilerPath": "clang++", + "C_Cpp_Runner.debuggerPath": "lldb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 00000000..4ede37a8 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,29 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: clang build active file", + "command": "/usr/bin/clang", + "args": [ + "-fcolor-diagnostics", + "-fansi-escape-codes", + "-g", + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/C++/Binary-Tree-Preorder-Traversal.cpp b/C++/Binary-Tree-Preorder-Traversal.cpp new file mode 100644 index 00000000..ed2c132e --- /dev/null +++ b/C++/Binary-Tree-Preorder-Traversal.cpp @@ -0,0 +1,27 @@ +/** + * 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 { +public: + vector preorderTraversal(TreeNode* root) { + vector node; + preorder(root, node); + return node; + } + private: + void preorder(TreeNode* root, vector& nodes){ + if(root == NULL) return; + + nodes.push_back(root->val); + preorder(root->left, nodes); + preorder(root->right, nodes); + } +}; \ No newline at end of file From 66fa44c45b58457c6edab2d04eafce193ba4af00 Mon Sep 17 00:00:00 2001 From: Monique Andrade Date: Fri, 21 Jun 2024 18:19:34 -0700 Subject: [PATCH 2/4] Added the iterative solution to #144 - Binary Tree Preorder Traversal --- .vscode/settings.json | 5 +++- C++/Binary-Tree-Preorder-Traversal.cpp | 35 ++++++++++++++++++-------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index b9c6ac87..ffc4cd67 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -55,5 +55,8 @@ "C_Cpp_Runner.useLeakSanitizer": false, "C_Cpp_Runner.showCompilationTime": false, "C_Cpp_Runner.useLinkTimeOptimization": false, - "C_Cpp_Runner.msvcSecureNoWarnings": false + "C_Cpp_Runner.msvcSecureNoWarnings": false, + "files.associations": { + "__locale": "cpp" + } } \ No newline at end of file diff --git a/C++/Binary-Tree-Preorder-Traversal.cpp b/C++/Binary-Tree-Preorder-Traversal.cpp index ed2c132e..add1ab2c 100644 --- a/C++/Binary-Tree-Preorder-Traversal.cpp +++ b/C++/Binary-Tree-Preorder-Traversal.cpp @@ -9,19 +9,34 @@ * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ +//This solution is an interative solution for problem #144 using stack +//Preorder Traversal is root, left, right class Solution { public: vector preorderTraversal(TreeNode* root) { - vector node; - preorder(root, node); - return node; - } - private: - void preorder(TreeNode* root, vector& nodes){ - if(root == NULL) return; + vector result; - nodes.push_back(root->val); - preorder(root->left, nodes); - preorder(root->right, nodes); + //if root == nullptr, return empty vector + if (root == NULL) { + return result; + } + + stack node_stack; + node_stack.push(root); + + while (!node_stack.empty()) { + TreeNode* node = node_stack.top(); + node_stack.pop(); //pop the top node from the stack + result.push_back(node->val); //add the value to the result vector + + if (node->right) { + node_stack.push(node->right); //push right child to the stack if it exists + } + if (node->left) { + node_stack.push(node->left); //push left child to the stack if it exists + } + } + + return result; } }; \ No newline at end of file From 4b5ab8972f4a40b00dc8f348dd6fb0747c74e292 Mon Sep 17 00:00:00 2001 From: mandradem <160947155+mandradem@users.noreply.github.com> Date: Fri, 21 Jun 2024 18:24:16 -0700 Subject: [PATCH 3/4] Update README.md Added the C++ solution link to problem #144 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b2cac739..007e5d48 100644 --- a/README.md +++ b/README.md @@ -248,7 +248,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 094 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Java](./Java/binary-tree-inorder-traversal.java)
[Python](./Python/Iterative-Inorder-tree-traversal) | _O(n)_ | _O(logn)_ | Medium | Binary Tree, Stack, HashTable | | | 100 | [Same Tree](https://leetcode.com/problems/same-tree/) | [Python](./Python/100.SymmetricTree.py)
[Java](./Java/Same-Tree.java) | _O(n)_ | _O(n)_ | Easy | Tree, Depth-first Search | | | 101 | [Symmetric Tree](https://leetcode.com/problems/symmetric-tree/) | [Java](./Java/symmetric-tree.java)
[Python](./Python/101.SymmetricTree.py) | _O(n)_ | _O(n)_ | Easy | Tree, Breadth-first Search, Depth-first Search | | -| 144 | [Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) | [Java](./Java/binary-tree-preorder-traversal.java) | _O(n)_ | _O(logn)_ | Medium | Binary Tree, Stack | | +| 144 | [Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) | [Java](./Java/binary-tree-preorder-traversal.java)
[C++](./C++/Binary-Tree-Preorder-Traversal.cpp) | _O(n)_ | _O(logn)_ | Medium | Binary Tree, Stack | | | 145 | [Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/) | [Java](./Java/binary-tree-postorder-traversal.java) | _O(n)_ | _O(logn)_ | Hard | Binary Tree, Stack | | | 103 | [ZigZag Level Order](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/) | [JavaScript](./JavaScript/Binary-Tree-ZigZag-Traversal.js)
[C++](./C++/binary-tree-preorder-traversal.java) | _O(n)_ | _O(n)_ | Medium | Binary Tree | | | 129 | [Sum Root to Leaf Numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers/) | [Java](./Java/sum-root-to-leaf-numbers.java) | _O(n)_ | _O(logn)_ | Medium | Binary Tree, Depth First Search | | From 2c654c96eb88a1c932c2143e6ebead6e3c48e279 Mon Sep 17 00:00:00 2001 From: mandradem <160947155+mandradem@users.noreply.github.com> Date: Fri, 21 Jun 2024 18:49:47 -0700 Subject: [PATCH 4/4] Update Binary-Tree-Preorder-Traversal.cpp Added the Space and Time-Complexity in the file --- C++/Binary-Tree-Preorder-Traversal.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/C++/Binary-Tree-Preorder-Traversal.cpp b/C++/Binary-Tree-Preorder-Traversal.cpp index add1ab2c..1694620b 100644 --- a/C++/Binary-Tree-Preorder-Traversal.cpp +++ b/C++/Binary-Tree-Preorder-Traversal.cpp @@ -11,6 +11,7 @@ */ //This solution is an interative solution for problem #144 using stack //Preorder Traversal is root, left, right +//The Space and Time Complexity is O(n) class Solution { public: vector preorderTraversal(TreeNode* root) { @@ -39,4 +40,4 @@ class Solution { return result; } -}; \ No newline at end of file +};