Skip to content

Commit c2dae7c

Browse files
committed
add two more
1 parent 3b8d940 commit c2dae7c

File tree

2 files changed

+196
-0
lines changed

2 files changed

+196
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//============================================================================
2+
// Populating Next Right Pointers in Each Node
3+
//
4+
// Given a binary tree
5+
//
6+
// struct TreeLinkNode {
7+
// TreeLinkNode *left;
8+
// TreeLinkNode *right;
9+
// TreeLinkNode *next;
10+
// }
11+
//
12+
// Populate each next pointer to point to its next right node. If there is no
13+
// next right node, the next pointer should be set to NULL.
14+
//
15+
// Initially, all next pointers are set to NULL.
16+
//
17+
// Note:
18+
//
19+
// You may only use constant extra space.
20+
// You may assume that it is a perfect binary tree (ie, all leaves are at the
21+
// same level, and every parent has two children).
22+
//
23+
// For example, given the following perfect binary tree:
24+
// " 1 "
25+
// " / \ "
26+
// " 2 3 "
27+
// " / \ / \ "
28+
// " 4 5 6 7 "
29+
// After calling your function, the tree should look like:
30+
// " 1 -> NULL "
31+
// " / \ "
32+
// " 2 -> 3 -> NULL "
33+
// " / \ / \ "
34+
// " 4->5->6->7 -> NULL "
35+
//============================================================================
36+
37+
#include <iostream>
38+
39+
using namespace std;
40+
41+
/**
42+
* Definition for binary tree with next pointer.
43+
*/
44+
struct TreeLinkNode
45+
{
46+
int val;
47+
TreeLinkNode *left, *right, *next;
48+
TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
49+
};
50+
51+
class Solution
52+
{
53+
public:
54+
void connect(TreeLinkNode *root)
55+
{
56+
//connect1(root);
57+
connect2(root);
58+
}
59+
60+
void connect1(TreeLinkNode *node)
61+
{
62+
if (node) node->next = NULL;
63+
connectHelper1(node);
64+
}
65+
66+
// BFS
67+
void connectHelper1(TreeLinkNode* node) {
68+
if (!node || !(node->left) || !(node->right)) return;
69+
TreeLinkNode* curr = node;
70+
while (curr) {
71+
if (curr->left) curr->left->next = curr->right;
72+
if (curr->right) curr->right->next = (curr->next) ? curr->next->left : NULL;
73+
curr = curr->next;
74+
}
75+
connectHelper1(node->left);
76+
}
77+
78+
void connect2(TreeLinkNode *node)
79+
{
80+
if (node) node->next = NULL;
81+
connectHelper2(node);
82+
}
83+
84+
// DFS
85+
void connectHelper2(TreeLinkNode* node) {
86+
if (!node || !(node->left) || !(node->right)) return;
87+
node->left->next = node->right;
88+
node->right->next = (node->next) ? node->next->left: NULL;
89+
connectHelper2(node->left);
90+
connectHelper2(node->right);
91+
}
92+
};
93+
94+
int main()
95+
{
96+
return 0;
97+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//============================================================================
2+
// Populating Next Right Pointers in Each Node II
3+
//
4+
// Follow up for problem "Populating Next Right Pointers in Each Node".
5+
//
6+
// What if the given tree could be any binary tree? Would your previous
7+
// solution still work?
8+
//
9+
// Note:
10+
//
11+
// You may only use constant extra space.
12+
//
13+
// For example, given the following perfect binary tree:
14+
// " 1 "
15+
// " / \ "
16+
// " 2 3 "
17+
// " / \ \ "
18+
// " 4 5 7 "
19+
// After calling your function, the tree should look like:
20+
// " 1 -> NULL "
21+
// " / \ "
22+
// " 2 -> 3 -> NULL "
23+
// " / \ \ "
24+
// " 4-> 5 -> 7 -> NULL "
25+
//============================================================================
26+
27+
#include <iostream>
28+
29+
using namespace std;
30+
31+
/**
32+
* Definition for binary tree with next pointer.
33+
*/
34+
struct TreeLinkNode
35+
{
36+
int val;
37+
TreeLinkNode *left, *right, *next;
38+
TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
39+
};
40+
41+
class Solution
42+
{
43+
public:
44+
void connect(TreeLinkNode *root)
45+
{
46+
connect1(root);
47+
}
48+
49+
void connect1(TreeLinkNode *curNode)
50+
{
51+
if (!curNode) return;
52+
TreeLinkNode* nextHead = NULL;
53+
while (curNode)
54+
{
55+
if(curNode->left)
56+
{
57+
curNode->left->next = getNextSibling(curNode, true);
58+
if (!nextHead) nextHead = curNode->left;
59+
}
60+
if (curNode->right)
61+
{
62+
curNode->right->next = getNextSibling(curNode, false);
63+
if (!nextHead) nextHead = curNode->right;
64+
}
65+
curNode = curNode->next;
66+
}
67+
connect1(nextHead);
68+
}
69+
70+
TreeLinkNode* getNextSibling(TreeLinkNode *curNode, bool isLeft)
71+
{
72+
if (isLeft)
73+
if (curNode->right) return curNode->right;
74+
while ((curNode = curNode->next))
75+
{
76+
if (curNode->left) return curNode->left;
77+
if (curNode->right) return curNode->right;
78+
}
79+
return NULL;
80+
}
81+
};
82+
83+
int main()
84+
{
85+
TreeLinkNode *root = new TreeLinkNode(3);
86+
root->left = new TreeLinkNode(9);
87+
root->right = new TreeLinkNode(20);
88+
root->right->left = new TreeLinkNode(15);
89+
root->right->right = new TreeLinkNode(7);
90+
91+
Solution sol;
92+
sol.connect(root);
93+
cout << root->val << endl;
94+
cout << root->left->val << endl;
95+
cout << root->left->next->val << endl;
96+
cout << root->right->left->val << endl;
97+
cout << root->right->left->next->val << endl;
98+
return 0;
99+
}

0 commit comments

Comments
 (0)