From 5b224797460816ace415acaa688f26f81f34f874 Mon Sep 17 00:00:00 2001 From: juan Date: Tue, 5 Jul 2022 09:08:49 +0800 Subject: [PATCH] vault backup: 2022-07-05 09:08:49 --- .../Leetcode Binary-Tree-Inorder-Traversal.md | 1 + ...eetcode Binary-Tree-Postorder-Traversal.md | 93 +++++++++++++++++++ ...Leetcode Binary-Tree-Preorder-Traversal.md | 1 + 3 files changed, 95 insertions(+) create mode 100644 CS notes/pages/Leetcode Binary-Tree-Postorder-Traversal.md diff --git a/CS notes/pages/Leetcode Binary-Tree-Inorder-Traversal.md b/CS notes/pages/Leetcode Binary-Tree-Inorder-Traversal.md index 13e4d5a..5b8a295 100644 --- a/CS notes/pages/Leetcode Binary-Tree-Inorder-Traversal.md +++ b/CS notes/pages/Leetcode Binary-Tree-Inorder-Traversal.md @@ -18,6 +18,7 @@ tag:#DFS ``` +- [[Leetcode Binary-Tree-Postorder-Traversal]] - [[Leetcode Binary-Tree-Preorder-Traversal]] diff --git a/CS notes/pages/Leetcode Binary-Tree-Postorder-Traversal.md b/CS notes/pages/Leetcode Binary-Tree-Postorder-Traversal.md new file mode 100644 index 0000000..f2efcd6 --- /dev/null +++ b/CS notes/pages/Leetcode Binary-Tree-Postorder-Traversal.md @@ -0,0 +1,93 @@ +# Leetcode Binary-Tree-Postorder-Traversal + +#### 2022-07-04 21:31 + +> ##### Algorithms: +> #algorithm #DFS #DFS_postorder +> ##### Data structures: +> #DS #binary_tree +> ##### Difficulty: +> #coding_problem #difficulty- +> ##### Additional tags: +> #leetcode #CS_list_need_practicing +> ##### Revisions: +> N/A + +##### Related topics: +```expander +tag:#DFS +``` + +- [[Leetcode Binary-Tree-Inorder-Traversal]] +- [[Leetcode Binary-Tree-Preorder-Traversal]] + + +##### Links: +- [Link to problem](https://leetcode.com/problems/binary-tree-postorder-traversal/) +___ +### Problem +https://leetcode.com/problems/binary-tree-postorder-traversal/ +#### Examples +**Example 1:** + +![](https://assets.leetcode.com/uploads/2020/08/28/pre1.jpg) + +**Input:** root = [1,null,2,3] +**Output:** [3,2,1] + +**Example 2:** + +**Input:** root = [] +**Output:** [] + +**Example 3:** + +**Input:** root = [1] +**Output:** [1] + +#### Constraints + +- The number of the nodes in the tree is in the range `[0, 100]`. +- `-100 <= Node.val <= 100` + +### Thoughts + +Same as [[Leetcode Binary-Tree-Inorder-Traversal]] and [[Leetcode Binary-Tree-Preorder-Traversal]] + +== TODO: write iteration and another algo #TODO == +### Solution + +Recursion +```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 { + void postorder(TreeNode *root, vector &answer) { + if (root == nullptr) { + return; + } + + postorder(root->left, answer); + postorder(root->right, answer); + answer.push_back(root->val); + } + +public: + vector postorderTraversal(TreeNode *root) { + vector answer; + postorder(root, answer); + return answer; + } +}; + +``` \ No newline at end of file diff --git a/CS notes/pages/Leetcode Binary-Tree-Preorder-Traversal.md b/CS notes/pages/Leetcode Binary-Tree-Preorder-Traversal.md index d9c3332..2d43fff 100644 --- a/CS notes/pages/Leetcode Binary-Tree-Preorder-Traversal.md +++ b/CS notes/pages/Leetcode Binary-Tree-Preorder-Traversal.md @@ -19,6 +19,7 @@ tag:#DFS ``` - [[Leetcode Binary-Tree-Inorder-Traversal]] +- [[Leetcode Binary-Tree-Postorder-Traversal]] ##### Links: