@@ -845,37 +845,33 @@ \subsubsection{描述}
845
845
846
846
847
847
\subsubsection {分析 }
848
- 要处理一个节点,可能需要最右边的兄弟节点,因此用广搜 。
848
+ 要处理一个节点,可能需要最右边的兄弟节点,首先想到用广搜。但广搜不是常数空间的,本题要求常数空间 。
849
849
850
850
注意,这题的代码原封不动,也可以解决 Populating Next Right Pointers in Each Node I.
851
851
852
+
852
853
\subsubsection {递归版 }
853
854
\begin {Code }
854
855
// LeetCode, Populating Next Right Pointers in Each Node II
855
- // 递归版, 时间复杂度O(n),空间复杂度O(n )
856
+ // 时间复杂度O(n),空间复杂度O(1 )
856
857
class Solution {
857
858
public:
858
859
void connect(TreeLinkNode *root) {
859
- vector<vector<TreeLinkNode *>> result;
860
- traverse(root, 1, result);
861
- // 开始修改next指针
862
- for (auto level : result) {
863
- for (auto iter = level.begin(); iter != prev(level.end()); ++iter)
864
- (*iter)->next = *next(iter);
865
- (*prev(level.end()))->next = nullptr;
866
- }
867
- }
868
-
869
- void traverse(TreeLinkNode *root, size_t level,
870
- vector<vector<TreeLinkNode *>> &result) {
871
- if (!root) return;
872
-
873
- if (level > result.size())
874
- result.push_back(vector<TreeLinkNode *>());
860
+ if (root == nullptr) return;
875
861
876
- result[level - 1].push_back(root);
877
- traverse(root->left, level + 1, result);
878
- traverse(root->right, level + 1, result);
862
+ TreeLinkNode dummy(-1);
863
+ for (TreeLinkNode *curr = root, *prev = &dummy;
864
+ curr; curr = curr->next) {
865
+ if (curr->left != nullptr){
866
+ prev->next = curr->left;
867
+ prev = prev->next;
868
+ }
869
+ if (curr->right != nullptr){
870
+ prev->next = curr->right;
871
+ prev = prev->next;
872
+ }
873
+ }
874
+ connect(dummy.next);
879
875
}
880
876
};
881
877
\end {Code }
@@ -884,32 +880,26 @@ \subsubsection{递归版}
884
880
\subsubsection {迭代版 }
885
881
\begin {Code }
886
882
// LeetCode, Populating Next Right Pointers in Each Node II
887
- // 迭代版, 时间复杂度O(n),空间复杂度O(n )
883
+ // 时间复杂度O(n),空间复杂度O(1 )
888
884
class Solution {
889
885
public:
890
886
void connect(TreeLinkNode *root) {
891
- vector<vector<int> > result;
892
- if (root == nullptr)
893
- return;
894
-
895
- vector<TreeLinkNode*> next, current;
896
- current.push_back(root);
897
- while (!current.empty()) {
898
- while (!current.empty()) {
899
- TreeLinkNode* node = current.front();
900
- current.erase(current.begin());
901
-
902
- if (!current.empty())
903
- node->next = current.front();
904
- else
905
- node->next = nullptr;
906
-
907
- if (node->left != nullptr)
908
- next.push_back(node->left);
909
- if (node->right != nullptr)
910
- next.push_back(node->right);
887
+ while (root) {
888
+ TreeLinkNode * next = nullptr; // the first node of next level
889
+ TreeLinkNode * prev = nullptr; // previous node on the same level
890
+ for (; root; root = root->next) {
891
+ if (!next) next = root->left ? root->left : root->right;
892
+
893
+ if (root->left) {
894
+ if (prev) prev->next = root->left;
895
+ prev = root->left;
896
+ }
897
+ if (root->right) {
898
+ if (prev) prev->next = root->right;
899
+ prev = root->right;
900
+ }
911
901
}
912
- swap(next, current);
902
+ root = next; // turn to next level
913
903
}
914
904
}
915
905
};
0 commit comments