-
Notifications
You must be signed in to change notification settings - Fork 160
/
Immediate right neighbor.cpp
89 lines (71 loc) · 2.16 KB
/
Immediate right neighbor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
Given a binary tree with parent link, find a node's immediate right neighbor without the root node.
*/
/*
solution: first find the lowest parent that both the node and its immediate neighbor have. Then
recursively find its immediate neighbor in the parent's left subtree.
O(h) time, O(1) space, h is the height of the node.
*/
#include<iostream>
using namespace std;
struct Node {
int val;
Node *left;
Node *right;
Node *parent;
Node(int v) : val(v), left(NULL), right(NULL), parent(NULL) {}
};
Node *FindNeighbor( Node *node, int level ) {
if( node == NULL ) return NULL;
if( level == 0 ) return node;
Node *left = FindNeighbor(node->left,level+1);
//always return left child at first such that this node is the immediate right neighbor
if( left ) {
return left;
} else {
return FindNeighbor(node->right,level+1);
}
}
Node *RightNeighbor(Node *node ){
Node *parent = node->parent;
//records the level of node in order to find the same level neighbor
int level = 0;
while (parent) {
//find the lowest parent for node and the its immediate right neighbor
while( parent && parent->left != node ) {
node = parent;
parent = node->parent;
level--;
}
if( parent == NULL )
return NULL;
//find the neighbor node with level 0
Node *local = FindNeighbor( parent->right, level);
if(local) {
return local;
} else{
node = parent;
parent = node->parent;
level--;
}
}
return NULL;
}
int main(){
Node *root = new Node(0);
root->left = new Node(1);
root->left->parent = root;
root->right = new Node(2);
root->right->parent = root;
root->left->left = new Node(3);
root->left->left->parent = root->left;
root->left->right = new Node(4);
root->left->right->parent = root->left;
root->right->right = new Node(5);
root->right->right->parent = root->right;
Node *p1 = root->left->left; //3
Node *p2 = root->left->right; //4
cout<<RightNeighbor(p1)->val<<endl;
cout<<RightNeighbor(p2)->val<<endl;
return 0;
}