Skip to content

Commit 5f53e67

Browse files
committed
couple of tree prolems
1 parent e8464dd commit 5f53e67

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* Definition for binary tree with next pointer.
3+
* struct TreeLinkNode {
4+
* int val;
5+
* TreeLinkNode *left, *right, *next;
6+
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
7+
* };
8+
*/
9+
10+
// With extra space. O(n) space. Iterative solution
11+
class Solution {
12+
public:
13+
void connect(TreeLinkNode *root) {
14+
15+
if(root == NULL)
16+
return;
17+
18+
queue<TreeLinkNode *> q;
19+
20+
q.push(root);
21+
22+
while(!q.empty()) {
23+
int num = q.size();
24+
while(num) {
25+
TreeLinkNode *cur = q.front();
26+
q.pop();
27+
num--;
28+
29+
if(num > 0)
30+
cur->next = q.front();
31+
else
32+
cur->next = NULL;
33+
34+
// assign next value
35+
36+
if(cur->left != NULL)
37+
q.push(cur->left);
38+
39+
if(cur->right != NULL)
40+
q.push(cur->right);
41+
}
42+
}
43+
44+
45+
}
46+
};
47+
48+
49+
------
50+
51+
// Recursive solution. Still uses extra space (extra space used to accomodate recursion stack)
52+
class Solution {
53+
public:
54+
void connect(TreeLinkNode *root) {
55+
56+
if(!root)
57+
return;
58+
59+
if(root->left) {
60+
root->left->next = root->right;
61+
if(root->next) {
62+
root->right->next = root->next->left;
63+
}
64+
65+
}
66+
connect(root->left);
67+
connect(root->right);
68+
}
69+
};
70+
71+
-----
72+
73+
// Two pointer method without any extra space
74+
class Solution {
75+
public:
76+
void connect(TreeLinkNode *root) {
77+
78+
TreeLinkNode *pre = root;
79+
TreeLinkNode *cur = NULL;
80+
81+
if(root == NULL)
82+
return;
83+
84+
while(pre && pre->left) {
85+
cur = pre;
86+
while (cur) {
87+
cur->left->next = cur->right;
88+
if(cur->next) {
89+
cur->right->next = cur->next->left;
90+
}
91+
cur = cur->next;
92+
}
93+
pre = pre->left;
94+
}
95+
}
96+
97+
};

right view of a binary tree.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
11+
/*
12+
Note to self : leveloftree needs to be passed as a pointer. Because otherwise,
13+
leveloftree takes the value of the previous self in the recursion tree. The line
14+
leveloftree won't take the value of curlevel unless its state is maintained through a pointer
15+
The other alternative is to drop the variable leveloftree and use
16+
result.size() < curlevel
17+
*/
18+
19+
class Solution {
20+
21+
22+
void printrightsideview(TreeNode *root, int curlevel, int *leveloftree, vector<int> &result) {
23+
if(root == NULL)
24+
return;
25+
26+
if(curlevel > *leveloftree ) {
27+
result.push_back(root->val);
28+
*leveloftree = curlevel;
29+
}
30+
31+
printrightsideview(root->right,curlevel+1,leveloftree,result);
32+
printrightsideview(root->left,curlevel+1,leveloftree,result);
33+
34+
}
35+
public:
36+
37+
vector<int> rightSideView(TreeNode* root) {
38+
39+
vector<int> result;
40+
int m = 0;
41+
printrightsideview(root, 1, &m, result);
42+
return result;
43+
44+
}
45+
46+
47+
};

0 commit comments

Comments
 (0)