File tree Expand file tree Collapse file tree 4 files changed +314
-0
lines changed Expand file tree Collapse file tree 4 files changed +314
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < vector>
3+
4+ using namespace std ;
5+
6+ class Node {
7+
8+ public:
9+ int value;
10+ Node* left;
11+ Node* right;
12+
13+ Node (int val) {
14+ this ->value = val;
15+ this ->left = NULL ;
16+ this ->right = NULL ;
17+ }
18+ };
19+
20+ vector< vector< int > > diaTree;
21+
22+ void diagonalTraversalUtil (Node* root, int index) {
23+ if (root == NULL ) {
24+ return ;
25+ }
26+
27+ if (index < diaTree.size ()) {
28+ diaTree[index].push_back (root->value );
29+ } else {
30+ vector<int > temp = {root->value };
31+
32+ diaTree.push_back (temp);
33+ }
34+ diagonalTraversalUtil (root->left , index+1 );
35+ diagonalTraversalUtil (root->right , index);
36+ }
37+
38+ void diagonalTraversal (Node* root) {
39+
40+ diagonalTraversalUtil (root, 0 );
41+
42+ for (int i = 0 ; i < diaTree.size (); i++) {
43+ cout << " Diagonal " << i+1 << " : " ;
44+ for (int j = 0 ; j < diaTree[i].size (); j++) {
45+ cout << diaTree[i][j] << " " ;
46+ }
47+ cout << endl;
48+ }
49+ }
50+
51+ int main () {
52+ Node* root = new Node (1 );
53+ root->left = new Node (2 );
54+ root->right = new Node (3 );
55+ root->left ->left = new Node (4 );
56+ root->left ->right = new Node (5 );
57+ root->right ->left = new Node (6 );
58+ root->right ->left ->left = new Node (7 );
59+
60+
61+ /*
62+ 1
63+ / \
64+ / \
65+ 2 3
66+ / \ |
67+ / \ |
68+ 4 5 /
69+ 6
70+ /
71+ 7
72+ */
73+
74+ diagonalTraversal (root);
75+
76+ return 0 ;
77+ }
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < deque>
3+
4+ using namespace std ;
5+
6+ class Node {
7+
8+ public:
9+ int value;
10+ Node* left;
11+ Node* right;
12+
13+ Node (int val) {
14+ this ->value = val;
15+ this ->left = NULL ;
16+ this ->right = NULL ;
17+ }
18+ };
19+
20+ deque <int > dq;
21+ int low, high;
22+
23+ void bottomViewUtil (Node* root, int horDist) {
24+ if (root == NULL ) {
25+ return ;
26+ }
27+
28+ if (!dq.empty () && horDist > high) {
29+ dq.push_back (root->value );
30+ high++;
31+ } else if (horDist < low) {
32+ dq.push_front (root->value );
33+ low--;
34+ } else {
35+ dq[horDist-low] = root->value ;
36+ }
37+ bottomViewUtil (root->left , horDist-1 );
38+ bottomViewUtil (root->right , horDist+1 );
39+ }
40+
41+ void bottomView (Node* root) {
42+ low = 0 ;
43+ high = -1 ;
44+
45+ bottomViewUtil (root, 0 );
46+
47+ deque <int > :: iterator i;
48+
49+ cout << " Bottom View: " ;
50+ for (i = dq.begin (); i != dq.end (); i++) {
51+ cout << *i << " " ;
52+ }
53+ cout << endl;
54+ }
55+
56+ int main () {
57+ Node* root = new Node (1 );
58+ root->left = new Node (2 );
59+ root->right = new Node (3 );
60+ root->left ->left = new Node (4 );
61+ root->left ->right = new Node (5 );
62+ root->right ->left = new Node (6 );
63+ root->right ->left ->left = new Node (7 );
64+
65+
66+ /*
67+ 1
68+ /.\
69+ 2 . 3
70+ /.\. |
71+ 4 . 5 |
72+ . ./
73+ . 6
74+ ./
75+ 7
76+ */
77+
78+ bottomView (root);
79+
80+ return 0 ;
81+ }
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+
3+ using namespace std ;
4+
5+ class Node {
6+
7+ public:
8+ int value;
9+ Node* left;
10+ Node* right;
11+
12+ Node (int val) {
13+ this ->value = val;
14+ this ->left = NULL ;
15+ this ->right = NULL ;
16+ }
17+ };
18+
19+ int maxLevel;
20+
21+ void leftViewUtil (Node* root, int level) {
22+ // Check if new level reached is the highest
23+ if (maxLevel < level) {
24+ cout << root -> value << endl;
25+ maxLevel = level;
26+ }
27+
28+ // Left side of the tree is traversed first so that
29+ // left nodes are checked first for the condition that
30+ // whether they have reached new max level or not.
31+ if (root -> left != NULL ) {
32+ leftViewUtil (root -> left, level+1 );
33+ }
34+
35+ if (root -> right != NULL ) {
36+ leftViewUtil (root -> right, level+1 );
37+ }
38+ }
39+
40+ void leftView (Node *root) {
41+ maxLevel = -1 ;
42+
43+ leftViewUtil (root, 0 );
44+ }
45+
46+ int main () {
47+ Node* root = new Node (1 );
48+ root->left = new Node (2 );
49+ root->right = new Node (3 );
50+ root->left ->left = new Node (4 );
51+ root->left ->right = new Node (5 );
52+ root->right ->left = new Node (6 );
53+ root->right ->left ->left = new Node (7 );
54+
55+
56+ /*
57+ -> 1
58+ / \
59+ / \
60+ / \
61+ -> 2 3
62+ / \ /
63+ -> 4 5 6
64+ /
65+ -> 7
66+ */
67+
68+ cout << " Left view of the tree: \n " ;
69+ leftView (root);
70+
71+ return 0 ;
72+ }
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < deque>
3+
4+ using namespace std ;
5+
6+ class Node {
7+
8+ public:
9+ int value;
10+ Node* left;
11+ Node* right;
12+
13+ Node (int val) {
14+ this ->value = val;
15+ this ->left = NULL ;
16+ this ->right = NULL ;
17+ }
18+ };
19+
20+ deque <int > dq;
21+ int low, high;
22+
23+ void topViewUtil (Node* root, int horDist) {
24+ if (root == NULL ) {
25+ return ;
26+ }
27+
28+ if (!dq.empty () && horDist > high) {
29+ dq.push_back (root->value );
30+ high++;
31+ } else if (horDist < low) {
32+ dq.push_front (root->value );
33+ low--;
34+ }
35+ topViewUtil (root->left , horDist-1 );
36+ topViewUtil (root->right , horDist+1 );
37+ }
38+
39+ void topView (Node* root) {
40+ low = 0 ;
41+ high = 0 ;
42+
43+ dq.push_back (root->value );
44+ topViewUtil (root, 0 );
45+
46+ deque <int > :: iterator i;
47+
48+ cout << " Bottom View: " ;
49+ for (i = dq.begin (); i != dq.end (); i++) {
50+ cout << *i << " " ;
51+ }
52+ cout << endl;
53+ }
54+
55+ int main () {
56+ Node* root = new Node (1 );
57+ root->left = new Node (2 );
58+ root->right = new Node (3 );
59+ root->left ->left = new Node (4 );
60+ root->left ->right = new Node (5 );
61+ root->right ->left = new Node (6 );
62+ root->right ->left ->left = new Node (7 );
63+
64+
65+ /*
66+ 1
67+ /.\
68+ / . \
69+ 2 . 3
70+ /.\ . |
71+ / . \. |
72+ 4 . 5 |
73+ . . /
74+ . ./
75+ . 6
76+ . /
77+ ./
78+ 7
79+ */
80+
81+ topView (root);
82+
83+ return 0 ;
84+ }
You can’t perform that action at this time.
0 commit comments