From 0efe4e243c6f2019e809e87c53ca2518db363750 Mon Sep 17 00:00:00 2001 From: Sainnhe Park Date: Tue, 31 Jan 2023 11:47:01 +0800 Subject: [PATCH] s0257 --- include/s0257_binary_tree_paths.hpp | 17 +++++++++++++++++ notes/src/btree_basic.md | 10 +++++++--- src/s0257_binary_tree_paths.cpp | 27 +++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 include/s0257_binary_tree_paths.hpp create mode 100644 src/s0257_binary_tree_paths.cpp diff --git a/include/s0257_binary_tree_paths.hpp b/include/s0257_binary_tree_paths.hpp new file mode 100644 index 0000000..e296f9a --- /dev/null +++ b/include/s0257_binary_tree_paths.hpp @@ -0,0 +1,17 @@ +#ifndef S0257_BINARY_TREE_PATHS_HPP +#define S0257_BINARY_TREE_PATHS_HPP + +#include +#include +#include + +#include "structures.hpp" + +using namespace std; + +class S0257 { + public: + vector binaryTreePaths(TreeNode* root); +}; + +#endif diff --git a/notes/src/btree_basic.md b/notes/src/btree_basic.md index ebf6f22..a1c3b87 100644 --- a/notes/src/btree_basic.md +++ b/notes/src/btree_basic.md @@ -2,7 +2,7 @@ ## 二叉树的种类 -满二叉树:如果一棵二叉树只有度为0的结点和度为2的结点,并且度为0的结点在同一层上,则这棵二叉树为满二叉树。 +满二叉树:如果一棵二叉树只有度为 0 的结点和度为 2 的结点,并且度为 0 的结点在同一层上,则这棵二叉树为满二叉树。 ![](https://img-blog.csdnimg.cn/20200806185805576.png) @@ -18,7 +18,7 @@ ![](https://img-blog.csdnimg.cn/20200806190304693.png) -平衡二叉搜索树:又被称为AVL(Adelson-Velsky and Landis)树,且具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。 +平衡二叉搜索树:又被称为 AVL(Adelson-Velsky and Landis)树,且具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过 1,并且左右两个子树都是一棵平衡二叉树。 ![](https://img-blog.csdnimg.cn/20200806190511967.png) @@ -39,7 +39,7 @@ struct TreeNode { ![](https://img-blog.csdnimg.cn/20200920200429452.png) -如果父节点的数组下标是 i,那么它的左孩子就是 i * 2 + 1,右孩子就是 i * 2 + 2。 +如果父节点的数组下标是 `i`,那么它的左孩子就是 `i * 2 + 1`,右孩子就是 `i * 2 + 2`。 ## 遍历方式 @@ -67,3 +67,7 @@ struct TreeNode { - 前序遍历:中左右 - 中序遍历:左中右 - 后序遍历:左右中 + +## 技巧 + +1. 深度优先搜索从下往上,广度优先搜索从上往下,所以如果需要处理从上往下并且状态积累的情形 (e.g. [s0404](https://leetcode.cn/problems/sum-of-left-leaves/) && [s0257](https://leetcode.cn/problems/binary-tree-paths/)) 可以先创建一个结构体用来描述节点状态,然后用 BFS 遍历。 diff --git a/src/s0257_binary_tree_paths.cpp b/src/s0257_binary_tree_paths.cpp new file mode 100644 index 0000000..5b10af4 --- /dev/null +++ b/src/s0257_binary_tree_paths.cpp @@ -0,0 +1,27 @@ +#include "s0257_binary_tree_paths.hpp" + +struct TreeNodeState_S0257 { + TreeNode* ptr; + string path; +}; + +vector S0257::binaryTreePaths(TreeNode* root) { + queue q; + vector result{}; + if (root) q.push(TreeNodeState_S0257{root, std::to_string(root->val)}); + while (!q.empty()) { + TreeNodeState_S0257 front = q.front(); + q.pop(); + if (front.ptr->left == nullptr && front.ptr->right == nullptr) + result.push_back(front.path); + if (front.ptr->left) + q.push(TreeNodeState_S0257{ + front.ptr->left, + front.path + "->" + std::to_string(front.ptr->left->val)}); + if (front.ptr->right) + q.push(TreeNodeState_S0257{ + front.ptr->right, + front.path + "->" + std::to_string(front.ptr->right->val)}); + } + return result; +}