From 10a4ad39507dc916a30e91ff2cbb9b9cb2c7bb6a Mon Sep 17 00:00:00 2001 From: CavelyDD Date: Tue, 11 Dec 2018 19:17:51 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E5=B1=82=E6=AC=A1=E9=81=8D=E5=8E=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + .../binary_tree_level_order_traversal.cpp | 96 +++++++++++++++++++ .../readme.md | 1 + LeafScar/readme.md | 6 +- 4 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 LeafScar/binary_tree_level_order_traversal/binary_tree_level_order_traversal.cpp create mode 100644 LeafScar/binary_tree_level_order_traversal/readme.md diff --git a/.gitignore b/.gitignore index a94212f..c7c7f14 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ *.opendb *.json *.sqlite +*.ipch diff --git a/LeafScar/binary_tree_level_order_traversal/binary_tree_level_order_traversal.cpp b/LeafScar/binary_tree_level_order_traversal/binary_tree_level_order_traversal.cpp new file mode 100644 index 0000000..1157551 --- /dev/null +++ b/LeafScar/binary_tree_level_order_traversal/binary_tree_level_order_traversal.cpp @@ -0,0 +1,96 @@ +/* + * Ⅰ.可以看成广度优先遍历 + * 创建一个队列 + * + * 3 + * / \ + * 9 20 + * / \ + * 15 7 + * 从根节点开始设为当前节点 + * 以下是队列的先入先出演示: + * + * 队列初始化,将根节点先入队 + * (1) 3 + * 访问时将当前节点出队,并将当前节点的左子与右子顺次入队 + * 出队顺序 + * (2) |20 9| 3(出) 3 + * (3) |20| 9(出) 9 + * (4) |7 15| 20(出) 20 + * (5) |7| 15(出) 15 + * (6) || 7(出) 7 + * + * 该算法可以以迭代的形式实现 + * 1.将当前节点赋值为队顶节点并访问 + * 2.将当前节点的左子入队 + * 3.将当前节点的右子入队 + * + * ☆.每次都是一层节点顺次入队完,再顺次入队该层节点的子节点,顺序不会打乱 + * 队列的特点是先入先出 + * + * + * Ⅱ.上述方法只能按层顺次访问,却无法分层,不能达到题目结果 + * 故,需要标记每一层的最后一个节点,当访问到时,即完成一层的访问,push到返回结果 + * + * Ⅰ中☆所述: 可以当访问完该层最后一个节点时, + * 同时会将最后一个节点的左子右子入队, + * 最后一个节点的右子正好是下一层的最后一个节点, + * 即可获得标记节点 + * + * 通过该标记节点起到分层作用,每次迭代更新 + */ +#include +#include +using namespace std; + +struct TreeNode { + int val; + TreeNode* left; + TreeNode* right; + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} +}; + +class Solution { +public: + vector> levelOrder(TreeNode* root) { + vector> rel; + + if (root == nullptr) + return rel; + + queue tempQueue; + + tempQueue.push(root); + TreeNode* lastNode = root; // 记录当前层的最后一个元素,即上一层顺次最后一个节点,push到队列中的最后一个孩子 + TreeNode* lastNodeSign = root; // 存储下一层的最后一个节点 + vector levelInt; + while (!tempQueue.empty()) + { + TreeNode* x = tempQueue.front(); + tempQueue.pop(); + + levelInt.push_back(x->val); // 存储访问元素的值 + + if (x->left != nullptr) + { + tempQueue.push(x->left); // 存入当前节点左子 + lastNodeSign = x->left; // 存入当前节点左子后,其正好为下一层的最后一个节点 + } + + if (x->right != nullptr) + { + tempQueue.push(x->right); // 存入当前节点右子 + lastNodeSign = x->right; // 下一层的最后一个节点变为其右子 + } + + if (x == lastNode) // 当访问到当前层最后一个元素时, + { + lastNode = lastNodeSign; // 更新lastNode为下一层的最后一个元素 + rel.push_back(levelInt); // levelInt已经包含当前层的所有值,存入返回结果中 + levelInt.clear(); // 清空levelInt,准备记录下一层的值 + } + } + + return rel; + } +}; diff --git a/LeafScar/binary_tree_level_order_traversal/readme.md b/LeafScar/binary_tree_level_order_traversal/readme.md new file mode 100644 index 0000000..516cbad --- /dev/null +++ b/LeafScar/binary_tree_level_order_traversal/readme.md @@ -0,0 +1 @@ +### 思路详见代码页 \ No newline at end of file diff --git a/LeafScar/readme.md b/LeafScar/readme.md index 01b15fb..3b46b18 100644 --- a/LeafScar/readme.md +++ b/LeafScar/readme.md @@ -2,4 +2,8 @@ -1.鏈闀垮叕鍏卞瓙搴忓垪 \ No newline at end of file +1.鏈闀垮叕鍏卞瓙搴忓垪 + +2.鎯呮劅涓板瘜鐨勬枃瀛 + +3. \ No newline at end of file From 6c0a3ce90ba1940f96226322ea544bcba425db44 Mon Sep 17 00:00:00 2001 From: CavelyDD Date: Tue, 11 Dec 2018 19:24:18 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E5=B1=82=E6=AC=A1=E9=81=8D=E5=8E=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LeafScar/readme.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/LeafScar/readme.md b/LeafScar/readme.md index 3b46b18..bfdbe6c 100644 --- a/LeafScar/readme.md +++ b/LeafScar/readme.md @@ -1,9 +1,7 @@ # LeafScar璐$尞鍐呭 - - 1.鏈闀垮叕鍏卞瓙搴忓垪 2.鎯呮劅涓板瘜鐨勬枃瀛 -3. \ No newline at end of file +3.浜屽弶鏍戠殑灞傛閬嶅巻 \ No newline at end of file