@@ -306,3 +306,118 @@ function hasDuplicateId(tree, idSet = new Set()) {
306306 return false ;
307307}
308308```
309+
310+ ### 11. 11. Implement a function to get all descendants of a given DOM node
311+
312+ - This function retrieves all child elements, regardless of depth.
313+
314+ ``` js copy
315+ /**
316+ * @param {HTMLElement | null} tree
317+ * @return {Array}
318+ */
319+ function getAllDescendants (root ) {
320+ if (! root) return [];
321+
322+ let descendants = [];
323+
324+ const helper = (node ) => {
325+ if (node) {
326+ descendants .push (node);
327+ if (node .children .length ) {
328+ for (let child of node .children ) {
329+ helper (child);
330+ }
331+ }
332+ }
333+ };
334+
335+ helper (root);
336+ return descendants;
337+ }
338+ ```
339+ ** References**
340+
341+ - https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/children
342+
343+ ---
344+
345+ ### 12. Implement a function to clone a DOM tree
346+
347+ - This function creates a deep copy of the given DOM node and its descendants.
348+
349+ ``` js copy
350+ /**
351+ * @param {HTMLElement | null} tree
352+ * @return {HTMLElement | null}
353+ */
354+ function cloneDOMTree (root ) {
355+ return root ? root .cloneNode (true ) : null ;
356+ }
357+
358+ ```
359+ ** References**
360+
361+ - https://developer.mozilla.org/en-US/docs/Web/API/Node/cloneNode
362+
363+ ---
364+
365+ ### 13. Implement a function to count all elements in a DOM tree
366+
367+ - This function counts all child elements, regardless of depth.
368+
369+ ``` js copy
370+ /**
371+ * @param {HTMLElement | null} tree
372+ * @return {number}
373+ */
374+ function countElements (root ) {
375+ if (! root) return 0 ;
376+
377+ let count = 1 ; // Count this element
378+
379+ if (root .hasChildNodes ()) {
380+ for (let child of root .children ) {
381+ count += countElements (child);
382+ }
383+ }
384+
385+ return count;
386+ }
387+ ```
388+ ---
389+ ### 14. Implement a function to serialize a DOM tree into a string
390+
391+ - This function converts a DOM tree into a string representation.
392+
393+ ``` js copy
394+ /**
395+ * @param {HTMLElement | null} tree
396+ * @return {string}
397+ */
398+ function serializeDOM (root ) {
399+ if (! root) return ' ' ;
400+
401+ let str = ` <${ root .tagName .toLowerCase ()} ` ;
402+
403+ for (let attr of root .attributes ) {
404+ str += ` ${ attr .name } ="${ attr .value } "` ;
405+ }
406+
407+ str += ' >' ;
408+
409+ if (root .hasChildNodes ()) {
410+ for (let child of root .children ) {
411+ str += serializeDOM (child);
412+ }
413+ }
414+
415+ str += ` </${ root .tagName .toLowerCase ()} >` ;
416+ return str;
417+ }
418+ ```
419+ ** References**
420+
421+ - https://developer.mozilla.org/en-US/docs/Web/API/Element/attributes
422+
423+ ---
0 commit comments