-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaryTree.js
1333 lines (1161 loc) · 33.6 KB
/
binaryTree.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 1) Preorder traversal
// Recursive Solution
const preorderTraversalRecursive = (root) => {
// Create a result array
let result = [];
preOrder(root);
// Result is automatically passed using the concept of lexical scoping
function preOrder(root) {
// Check if node exist || root !== null
if (root) {
// Push into result
result.push(root.val);
preOrder(root.left);
preOrder(root.right);
}
}
return result;
};
// Iterative solution
const preorderTraversalIterative = (root) => {
// Defining the result identifier array
const result = [];
// Stack to store untraversed elements
const stack = [];
// Loop
while (root !== null || stack.length !== 0) {
if (root === null) {
root = stack.pop();
}
result.push(root.val);
if (root.right) {
stack.push(root.right);
}
root = root.left;
}
return result;
};
// 2) InOrder Traversal
// Recursive Solution
const inorderTraversalRecursive = (root) => {
// define an array identifier
const result = [];
inOrder(root);
function inOrder(root) {
if (!root) return;
inOrder(root.left);
result.push(root.val);
inOrder(root.right);
}
return result;
};
// Iterative Solution
const inorderTraversalIterative = (root) => {
const result = [];
const stack = [];
while (root !== null || stack.length !== 0) {
while (root !== null) {
stack.push(root);
root = root.left;
}
root = stack.pop();
result.push(root.val);
root = root.right;
}
return result;
};
// 3) PostOrder traversal
// Recursive Approach
const postorderTraversalRecursive = (root) => {
// Define an Identifier Result
const result = [];
postOrder(root);
function postOrder(root) {
if (!root) return;
postOrder(root.left);
postOrder(root.right);
result.push(root.val);
}
return result;
};
// Iterative
const postorderTraversalIterative = (root) => {
// Define an Identifier Result
const result = [];
const stack = [root];
while (stack.length !== 0) {
let root = stack.pop();
result.push(root.val);
if (root.right) {
stack.push(root.right);
}
if (root.left) {
stack.push(root.left);
}
return result;
}
};
// 4) Maximum Depth of Binary Tree
const maxDepth = (root) => {
// Check for edge case (no root)
if (!root) return 0;
// Now recursively traverse both the left and right subtree
let leftTree = maxDepth(root.left);
let rightTree = maxDepth(root.right);
// Now compare both to find Max
return Math.max(leftTree + 1, rightTree + 1);
};
// 5) Maximum Depth of N-ary Tree (Can have n number of child nodes)
const maxDepthNAry = (root) => {
// Check for edge cases (when no root)
if (!root) return 0;
// Let the initial answer be 0 for each root node
let ans = 0;
// Loop over each node
for (let x of root.children) {
ans = Math.max(ans, maxDepth(x));
}
return ans + 1;
};
// 6) Merge Binary Trees
const mergeTrees = (root1, root2) => {
// Check if both don't exist (edge case)
if (!root1 && !root2) return null;
// Check if either exist at that node (simply return)
if (!root1 || !root2) return root1 || root2;
// Summation of node
root1.val += root2.val;
// Now traverse left and then right in sequence
root1.left = mergeTrees(root1.left, root2.left);
root1.right = mergeTrees(root1.right, root2.right);
return root1;
};
// 7) IsUniversal Tree
const isUnivalTree = (root) => {
// Define Identifier value and store the root value
let value = root.val;
function checkIfTrue(root) {
if (!root) return true;
let leftNode = checkIfTrue(root.left);
let rightNode = checkIfTrue(root.right);
return leftNode && rightNode && root.val === value;
}
// Call the function to check if true
return checkIfTrue(root);
};
// 8) Compare Leaves
const leafSimilar = (root1, root2) => {
const leaves1 = [];
const leaves2 = [];
function checkForLeaves(root, leaves) {
if (!root) return;
checkForLeaves(root.left, leaves);
checkForLeaves(root.right, leaves);
if (!root.left && !root.right) {
leaves.push(root.val);
}
}
// Find all the leaves for tree 1
checkForLeaves(root1, leaves1);
checkForLeaves(root2, leaves2);
if (leaves1.length !== leaves2.length) return false;
for (let i = 0; i < leaves1.length; i++) {
if (leaves1[i] !== leaves2[i]) {
return false;
}
}
return true;
};
// 9) Count left Leaves only
// O(n) & O(n) solution
const sumOfLeftLeaves = (root) => {
// In case there is no left or right
if (!root.left && !root.right) {
return 0;
}
// Declare identifiers
let leaves = [];
function countLeftLeaves(root, leaves, toCount) {
if (!root) {
return;
}
countLeftLeaves(root.left, leaves, true);
countLeftLeaves(root.right, leaves, false);
if (!root.left && !root.right && toCount) {
leaves.push(root.val);
return;
}
}
countLeftLeaves(root, leaves, true);
let sum = 0;
for (let x of leaves) {
sum += x;
}
return sum;
};
// O(n) solution
const sumOfLeftLeavesBest = (root) => {
// Check if no left and right
if (!root.left && !root.right) {
return 0;
}
// Calculate the sum
let sum = 0;
let isLeft = true;
function countLeftLeaves(root, isLeft) {
if (!root) return;
countLeftLeaves(root.left, true);
countLeftLeaves(root.right, false);
if (!root.left && !root.right && isLeft) {
sum = sum + root.val;
return;
}
}
countLeftLeaves(root, isLeft);
return sum;
};
// 10) Check if both tree are same
const isSameTree = (p, q) => {
// Check if both tree are empty
if (!p && !q) return true;
// Check if either of them exist
if (!p || !q) return false;
// Define the identifiers required
let tree1 = [];
let tree2 = [];
// Define the function required
function isSame(root, arr) {
// Define the termination case
if (!root) {
arr.push(null);
return;
}
// Simply push the node value into the array
arr.push(root.val);
// Traverse left and right
isSame(root.left, arr);
isSame(root.right, arr);
}
// call or invoke the function
isSame(p, tree1);
isSame(q, tree2);
// Check if both tree have equal number of nodes
if (tree1.length !== tree2.length) return false;
// Now traverse to compare the values of node
for (let i = 0; i < tree1.length; i++) {
if (tree1[i] !== tree2[i]) {
return false;
}
}
return true;
};
// Optimized SOlution O(n) O(1)
const isSameTreeOptimized = (p, q) => {
// Check for edge cases
// If neither p exist nor q exist
if (!p && !q) {
return true;
}
// If either p exist or q exist return the one that exist. Whichever is not null
if (!p || !q) {
return false;
}
return (
p.val === q.val &&
isSameTreeOptimized(p.left, q.left) &&
isSameTreeOptimized(p.right, q.right)
);
};
// 11) Invert Binary Tree
const invertTree = (root) => {
if (!root) return root;
function invert(root) {
if (!root) return;
let temp = root.left;
root.left = root.right;
root.right = temp;
invertTree(root.left);
invertTree(root.right);
return root;
}
return invert(root);
};
// 12) Univalued longest path
const longestUnivaluePath = (root) => {
// Check for edge case
if (!root) return 0;
// Defining identifiers required. Max for storing result
let max = 0;
// To recursively calculate the length of left and right subtree
let left = 0,
right = 0;
// function for finding the path
function longestPath(root) {
// base condition to break the stack
if (!root) {
return 0;
}
// Recursively call left and right
left = longestPath(root.left);
right = longestPath(root.right);
// Check if univalued path or not
if (!root.left || root.left.val !== root.val) {
left = 0;
}
if (!root.right || root.right.val !== root.val) {
right = 0;
}
// Find the new max
max = Math.max(max, left + right);
// Sending back the highest of left or right if there.. +1 in case they match
return Math.max(left, right) + 1;
}
longestPath(root);
return max;
};
// 13) Cousins in Binary Tree
const isCousins = (root, x, y) => {
let left = [];
let right = [];
let depth = -1;
function checkSibling(root, x, y, depth) {
if (!root) return;
depth++;
checkSibling(root.left, x, y, depth);
checkSibling(root.right, x, y, depth);
// Check the parent and depth
if (root.left) {
if (root.left.val === x) {
left.push(root.val, depth);
} else if (root.left.val === y) {
right.push(root.val, depth);
}
}
if (root.right) {
if (root.right.val === x) {
left.push(root.val, depth);
} else if (root.right.val === y) {
right.push(root.val, depth);
}
}
return;
}
checkSibling(root, x, y, depth);
return left[0] !== right[0] && left[1] === right[1] ? true : false;
};
//14) Find the right view of the tree
function rightView(root) {
// Define the identifiers required
let level = 0;
let result = [];
function findView(root, level) {
// If there is no root/node
if (!root) {
return null;
}
// Check if first node on this level
if (level === result.length) {
result.push(root.data);
}
// Traverse to right first
findView(root.right, level + 1);
findView(root.left, level + 1);
}
findView(root, level);
return result;
}
// 15) Top View
function topView(root) {
// Declare and Initiate identifiers required
// We will first create a queue for level order traversing
let queue = [];
let ans = [];
// Check for edge cases
if (!root) return ans;
// A map to find the top view
let topElements = {};
// push first level in form of [root, vertical level/ line (initially 0)]
queue.push([root, 0]);
// Loop until the end of queue
while (queue.length > 0) {
// find the top most node (first element pushed = arr[0])
let top = queue[0];
// Now pop the element
queue.shift();
// Find the node value and line number using array destructuring
let node = top[0];
let lineNumber = top[1];
// Check if an element on the same line number already exist on map or store it
if (!topElements[lineNumber]) {
topElements[lineNumber] = node;
}
// Now push whatever is on the left of current root to queue
if (node.left) {
queue.push([node.left, lineNumber - 1]);
}
// Now push whatever is on the right of current root to queue
if (node.right) {
queue.push([node.right, lineNumber + 1]);
}
}
// Get the object keys and sort them in order
let keys = Object.keys(topElements).sort((a, b) => a - b);
// Now fill the answer array
for (let i = 0; i < keys.length; i++) {
// Now store in right way
ans.push(topElements[keys[i]].data);
}
return ans;
}
// 16) Pre, Inorder and postOrder
const rightSideView = (root) => {
// Check for edge case - no pre, in or post order
if (!root) return [];
// Declare and initiate identifiers required to store results
let inOrder = [];
let preOrder = [];
let postOrder = [];
// Defining stack required to store node on traversals [node, (order no)]
let stack = [];
// Insert root Node with order No 1 for preOrder
stack.push({ node: root, orderNumber: 1 });
while (stack.length > 0) {
// Select node from top
let currNode = stack[stack.length - 1];
stack.pop();
// If part of preOrder
// Increment 1 to 2 and push the left side of tree
if (currNode.orderNumber === 1) {
preOrder.push(currNode.node.val);
currNode.orderNumber++;
stack.push({ node: currNode.node, orderNumber: currNode.orderNumber });
// push if left exist
if (currNode.node.left !== null) {
stack.push({ node: currNode.node.left, orderNumber: 1 });
}
} else if (currNode.orderNumber === 2) {
inOrder.push(currNode.node.val);
currNode.orderNumber++;
stack.push({ node: currNode.node, orderNumber: currNode.orderNumber });
// Check if node left available
if (currNode.node.right !== null) {
stack.push({ node: currNode.node.right, orderNumber: 1 });
}
} else {
postOrder.push(currNode.node.val);
}
}
return [preOrder, inOrder, postOrder];
};
// 17) Vertical traversal of a Tree
// This is a BFS Solution. TC - O(N*NLOGN*N) SC-O(N)
const verticalTraversal = (root) => {
// check for edge case (in case no root)
if (!root) return [];
// Define the identifiers required for the solution
// We need a queue for level order or Breadth First Search
const queue = [];
// We will store the traversed node in the list
const list = [];
// Initialize the queue with the root element.. pattern [node, row, col]
// Our main criteria of difference is col
queue.push([root, 0, 0]);
// Loop until the queue is empty/ Entire tree traversed
while (queue.length > 0) {
// Select the current node, row and col for traversing and storing in list
// arr.shift() method remove the element from start
let [node, row, col] = queue.shift();
// Check if left node exist... we decrement col to left
if (node.left) {
queue.push([node.left, row + 1, col - 1]);
}
// Check if right node exist.. We increment col to right
if (node.right) {
queue.push([node.right, row + 1, col + 1]);
}
// Push the current Node onto the list
list.push([node.val, row, col]);
}
// Now sort the store nodes in the list are complete traversal of tree
list.sort((a, b) => {
// First compare their cols.. If they are in same col check row
if (a[2] - b[2] === 0) {
// We can also say a[2] === b[2] in same cols
// Check their row.. If in the same row level we compare value
if (a[1] - b[1] === 0) {
// We can also say a[1] === b[1] in same rows
return a[0] - b[0];
}
}
return a[2] - b[2];
});
// Storing it onto a map as Map only take unique keys as well as O(1) Lookup being unordered
const map = new Map();
// Loop through the entire sorted list
for (let i = 0; i < list.length; i++) {
// get the currennt Node, col and it's row
let [value, row, col] = list[i];
// Check the map if col already exist and then simply push the value
if (map.has(col)) {
// Fetch the current col.. The value stored in col key which is an array of node values
map.get(col).push(value);
} else {
map.set(col, [value]);
}
}
// Since map.values() returns us an array and we spread it.. Thus returning format required with right sequence of nodes
return [...map.values()];
};
// 18) Root to node Path
// Brute Force Solution TC - O(N) SC - O(N) in case we consider result array
function findPath(A, B) {
// Check if empty tree
if (!A) return [];
// Define the identifiers required to solve the problem
// Initialize it with root
let path = [];
// Define the recursive function to find path
function findPath(A) {
// Define the termination case
if (!A) return 0;
// Check if this is the target node
if (A.data === B) {
return 1;
}
// Traverse left and then right
let left = findPath(A.left);
let right = findPath(A.right);
// Check if either left or right is 1
if (left === 1) {
path.push(A.left.data);
}
if (right === 1) {
path.push(A.right.data);
}
return left || right;
}
findPath(A);
path.push(A.data);
path.reverse();
return path;
}
// O(N) Optimized
function findPathOptimized(A, B) {
// Check if empty tree
if (!A) return [];
// Define the identifiers required to solve the problem
// Initialize it with root
let path = [];
// Define the recursive function to find path
function findPath(A) {
// Define the termination case
if (!A) return 0;
// Push the current Node val in path array
path.push(A.data);
// Check if this is the target node
if (A.data === B) {
return 1;
}
// Traverse left and then right
let left = findPath(A.left);
let right = findPath(A.right);
// Check if both zero
if (left === 0 && right === 0) {
path.pop();
}
return left || right;
}
findPath(A);
return path;
}
// 19) Morris Traversal Inorder
const inorderTraversal = (root) => {
// Morris Traversal
// Check for edge cases
if (!root) return [];
// define identifiers required
let curr = root;
let inorder = [];
// Loop until curr is null/does not exist
while (curr !== null) {
// Check if there is no left to curr... i.e is root
if (curr.left === null) {
inorder.push(curr.val);
// Move it to right
curr = curr.right;
} else {
// If there is a left we need to store the root reference in prev identifier
let prev = curr.left;
// Now we will move as much right as possible to the prev.
// prev must not point to curr which is the root of prev. i.e the overall root
while (prev.right !== null && prev.right !== curr) {
prev = prev.right;
}
// Check if reached end of right connect it to curr or root of the subtree
if (prev.right === null) {
prev.right = curr;
// continue traversal
curr = curr.left;
} else {
// Since the node was connected to root it was revisited so break it and push it
prev.right = null;
// push the root into inorder
inorder.push(curr.val);
curr = curr.right;
}
}
}
return inorder;
};
// 20) Binary Tree Level Order traversal... Both TC - O(N) SC - O(N)
// The solution where we reduce the while loop calls with for loop. We can differencitate the level of nodes
const widthOfBinaryTree = (root) => {
// Define the condition for edge cases
if (!root) return 0;
// Declare and initiate the identifiers required
const queue = []; // We define a queue for level order traversal.
let min; // It stores the min index at each level
let size; // The size of queue defines the number of node at each level
let width = 0; // Finding the width
// Push the first node onto the queue.. Root node. Form is [node, index]
queue.push([root, 0]);
// Now in order to do level order traversal loop until queue is empty
while (queue.length > 0) {
// get the size at each level.. On each traversal of while we traverse all nodes on a particular level
size = queue.length;
// Finding the min index for subtraction during push
min = queue[0][1];
// They store the first and last element at each level
let first, last;
// Loop over the all the nodes on the level
for (let i = 0; i < size; i++) {
// Get the curr node information
let [node, currIdx] = queue.shift();
// Decrease the currIdx by min. To ensure all id start from 0.
currIdx -= min;
// Check if index i === 0. It is the first node and i === size - 1 it is last node. Store Idx
if (i === 0) first = currIdx;
if (i === size - 1) last = currIdx;
// Check if the current Node has a left
if (node.left) {
queue.push([node.left, currIdx * 2 + 1]);
}
// Check if the current Node has a right
if (node.right) {
queue.push([node.right, currIdx * 2 + 2]);
}
}
width = Math.max(width, last - first + 1);
}
return width;
};
// The solution without the use of for loop. In this we are not able to differencitate between the level of nodes
const levelOrder = function (root) {
// Check for the edge case
if (!root) {
return [];
}
// Define the identifiers required
let queue = []; // We need a queue for level order traversal
let list = []; // The final list which will be returned
// Initiate the queue with root node
queue.push(root);
// Loop until the queue is empty
while (queue.length > 0) {
// The current Node
let node = queue.shift();
// Check if it has a left or right
if (node.left) {
queue.push(node.left);
}
if (node.right) {
queue.push(node.right);
}
list.push(node.val);
}
return list;
};
// 21) Diameter of Binary Tree (Basically the longest distance between two nodes) TC - O(N) SC - O(1)
const diameterOfBinaryTree = (root) => {
// check for edge case
if (!root) return 0;
// Identifier required
var diameter = 0;
function findDiameter(root) {
if (!root) return 0;
let left = findDiameter(root.left);
let right = findDiameter(root.right);
diameter = Math.max(diameter, left + right);
return Math.max(left, right) + 1;
}
findDiameter(root);
return diameter;
};
// 22) Is Balanced or Not TC - O(N) and SC - O(1)
const isBalanced = (root) => {
// Check for edge case
if (!root) return true;
// Define the identifiers required
var balanced = true;
function check(root) {
if (!root) return 0;
let left = check(root.left);
let right = check(root.right);
// Check if unbalanced
if (Math.abs(left - right) > 1 || Math.abs(right - left) > 1) {
balanced = false;
}
return Math.max(left, right) + 1;
}
check(root);
return balanced;
};
// 23) LCA or lowest common ancestor
// Brute Force Solution TC - O(N + N + N) and SC - O(N)
const lowestCommonAncestor = (root, p, q) => {
// check for edge case
if (!root) return;
// define the identifiers required
let firstPath = [];
let secondPath = [];
var ans;
// Define a function to find the path
function findPath(root, array, target) {
// Define the termination case of recursion
if (!root) return 0;
// Push the current Node onto array
array.push(root);
// Check if the target root if found
if (root === target) {
return 1;
}
let left = findPath(root.left, array, target);
let right = findPath(root.right, array, target);
// check if leaf node
if (left === 0 && right === 0) {
array.pop();
}
return left || right;
}
// find both path
findPath(root, firstPath, p);
findPath(root, secondPath, q);
// Now loop through both of them and try to find a non unique element
let index = 0;
while (index < firstPath.length || index < secondPath.length) {
if (firstPath[index] !== secondPath[index]) {
return firstPath[index - 1];
}
index++;
}
};
// 24) Binary ZigZag Level Order TC - O(N) SC - O(N)
const zigzagLevelOrder = (root) => {
// Define the edge cases
if (!root) return [];
// Define the identifiers required
let isLeftToRight = true; // To check wether to store left to right or right to left
const Queue = []; // We use double ended queue
const list = []; // Final list to store the levels of nodes
let size, node;
// Initiate the queue with Root
Queue.push(root);
// Define the loop until queue empty
while (Queue.length > 0) {
// Find the size of queue
size = Queue.length;
// List to store the current level
let level = [];
// Loop over the entire queue
for (let i = 0; i < size; i++) {
// Check the first node
let node = Queue.shift();
// Now find the index where to insert
let index = isLeftToRight ? i : size - 1 - i;
// Check if it has a left
if (node.left) {
Queue.push(node.left);
}
// Check if it has a right
if (node.right) {
Queue.push(node.right);
}
// Now insert the current node into level list
level[index] = node.val;
}
// Now push the level onto list
list.push(level);
// Increment isLeft
isLeftToRight = !isLeftToRight;
}
return list;
};
// 25) Boundary of a Tree
function boundaryOfBinaryTree(root) {
// Define the edge cases
if (!root) return [];
// Define the identifiers required
let stack = [];
let curr = root.left;
// Insert Root onto the Stack
stack.push(root.val);
// Define the Loop to findLeft
while (curr) {
// Check if current is not leaf Node
if (!(curr.left === null && curr.right === null)) {
stack.push(curr.val);
}
// Insert the left or right node
if (curr.left) {
curr = curr.left;
} else {
curr = curr.right;
}
}
// Define the loop for all leaf nodes
curr = root;
function findLeaves(curr) {
// Define the case of termination
if (!curr) return;
// Check if leaf node
if (curr.left === null && curr.right === null) {
stack.push(curr.val);
}
findLeaves(curr.left);
findLeaves(curr.right);
return;
}
findLeaves(curr);
// Define the loop to findRight
// Take a temorary array to store values
curr = root.right;
let temp = [];
while (curr) {
if (!(curr.left === null && curr.right === null)) {
temp.push(curr.val);
}
if (curr.right) {
curr = curr.right;
} else {
curr = curr.left;
}
}
// Now store the right into result array
let len = stack.length;
for (let i = temp.length - 1; i >= 0; i--) {
stack.push(temp[i]);
}
return stack;
}
// 26) Max Path Sum TC - O(N) SC - O(1)
const maxPathSum = (root) => {
// check for edge case
if (!root) return 0;
// Define the identifiers required
let sum = -Infinity;
function findSum(root) {
// check for the case of termination
if (!root) return 0;
// Traverse to left
// We will ignore the negative paths as it won't help in sum
let leftSum = Math.max(0, findSum(root.left));
let rightSum = Math.max(0, findSum(root.right));
// Find the sum
sum = Math.max(sum, leftSum + rightSum + root.val);
// return the current max
return root.val + Math.max(leftSum, rightSum);
}
findSum(root);