Skip to content

Commit 473299c

Browse files
committed
Populating Next Right Pointers in Each Node II 要求常数空间
1 parent 1034859 commit 473299c

File tree

2 files changed

+33
-43
lines changed

2 files changed

+33
-43
lines changed

C++/LeetCodet题解(C++版).pdf

-462 Bytes
Binary file not shown.

C++/chapTree.tex

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -845,37 +845,33 @@ \subsubsection{描述}
845845

846846

847847
\subsubsection{分析}
848-
要处理一个节点,可能需要最右边的兄弟节点,因此用广搜
848+
要处理一个节点,可能需要最右边的兄弟节点,首先想到用广搜。但广搜不是常数空间的,本题要求常数空间
849849

850850
注意,这题的代码原封不动,也可以解决 Populating Next Right Pointers in Each Node I.
851851

852+
852853
\subsubsection{递归版}
853854
\begin{Code}
854855
// LeetCode, Populating Next Right Pointers in Each Node II
855-
// 递归版,时间复杂度O(n),空间复杂度O(n)
856+
// 时间复杂度O(n),空间复杂度O(1)
856857
class Solution {
857858
public:
858859
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;
875861

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);
879875
}
880876
};
881877
\end{Code}
@@ -884,32 +880,26 @@ \subsubsection{递归版}
884880
\subsubsection{迭代版}
885881
\begin{Code}
886882
// LeetCode, Populating Next Right Pointers in Each Node II
887-
// 迭代版,时间复杂度O(n),空间复杂度O(n)
883+
// 时间复杂度O(n),空间复杂度O(1)
888884
class Solution {
889885
public:
890886
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+
}
911901
}
912-
swap(next, current);
902+
root = next; // turn to next level
913903
}
914904
}
915905
};

0 commit comments

Comments
 (0)