Labs
Quickstart Trees on LeetCode

Quickstart Trees on LeetCode

<p>This is a beginner lab and primer to start solving questions on trees in Data Structures and Algorithms.</p> <p><span style="background-color: #c2e0f4;"><strong>What is a tree?</strong></span></p> <p>A tree is a data structure that looks like this:</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4️⃣&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3️⃣&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5️⃣</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/&nbsp; &nbsp; &nbsp; \&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /&nbsp; &nbsp; &nbsp;\</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; 1️⃣&nbsp; &nbsp; &nbsp; &nbsp; 2️⃣&nbsp; &nbsp; &nbsp;6️⃣&nbsp; &nbsp; &nbsp;7️⃣&nbsp;</p> <p><span style="background-color: #c2e0f4;"><strong>Why does it look like this?</strong></span></p> <p>In the above representation, it is what we call a tree and each point holding a value is called a node. A node is classified depending on it's placement in the structure.&nbsp;</p> <p>Node with value <span style="background-color: #f8cac6;">4</span> --&gt; called root node coz it has no parent above it</p> <p>Node with value <span style="background-color: #fbeeb8;">3</span> --&gt; is left child of root node, sibling of node with value 5 and parent node of nodes with value 1 and 2.</p> <p>Node with value <span style="background-color: #bfedd2;">1</span> --&gt; is left child of node with value 3, sibling of node with value 2 and also called leaf node as it doesn't have any children.</p> <p>Node with value <span style="background-color: #bfedd2;">2</span> --&gt; is right child of node with value 3, sibling of node with value 1 and also called leaf node as it doesn't have any children.</p> <p>Node with value <span style="background-color: #fbeeb8;">5</span> --&gt; is right child of root node, sibling of node with value 3 and parent node of nodes with value 6 and 7.</p> <p>Node with value <span style="background-color: #bfedd2;">6</span> --&gt; is left child of node with value 5, sibling of node with value 7 and also called leaf node as it doesn't have any children.</p> <p>Node with value <span style="background-color: #bfedd2;">7</span> --&gt; is right child of node with value 5, sibling of node with value 7 and also called leaf node as it doesn't have any children.</p> <p>Let's summarize!</p> <p>We have</p> <ul> <li>four leaf nodes -&gt; 1, 2, 6, 7</li> <li>three parent nodes -&gt; 4, 3, 5</li> <li>one root node -&gt; 4</li> <li>one tree -&gt; type binary as there are maximum two children nodes of parent nodes</li> </ul> <p><span style="background-color: #c2e0f4;"><strong>Why do we need to traverse?</strong></span></p> <ul> <li>To find out tree node value</li> <li>To make calculations on the basis of tree node value</li> </ul> <p><span style="background-color: #c2e0f4;"><strong>How to define node using code? (naiice, it rhymes :p)</strong></span></p> <pre class="language-python"><code>class Node: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right</code></pre> <p><span style="background-color: #c2e0f4;"><strong>What are the algorithms to traverse trees?</strong></span></p> <p>There are many but we will look at three ways to walk through a tree.</p> <ul style="list-style-type: square;"> <li>Pre-order</li> <li>In-order</li> <li>Post-order</li> <li>Level-order</li> </ul>

2

Mathematics of height and nodes in Trees and Heaps

Mathematics of height and nodes in Trees and Heaps

<p>The <span style="background-color: #eccafa;">height of a node</span> in tree:</p> <p>- is the length of the path from that node to the deepest node in the tree.</p> <p>- is zero for a tree with only one node (i.e. root node).</p> <p>&nbsp;</p> <p>The <span style="background-color: #c2e0f4;">height of tree</span>:</p> <p>- is the maximum height among all the nodes in the tree.</p> <p>- is not the same as the height of a node in a tree unless, it is a root node.</p> <p>- is equal to the depth of the tree (in value but not in concept and it can be different for height of node).</p> <p>- is represented as <em>h</em>, here.</p> <p>&nbsp;</p> <p>Now, let's see the height:</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4️⃣&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;- &gt; Height = 2 for all nodes at this level</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3️⃣&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5️⃣&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - &gt; Height = 1 for all nodes at this level</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/&nbsp; &nbsp; &nbsp; \&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /&nbsp; &nbsp; &nbsp;\</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; 1️⃣&nbsp; &nbsp; &nbsp; &nbsp; 2️⃣&nbsp; &nbsp; &nbsp;6️⃣&nbsp; &nbsp; &nbsp; &nbsp;7️⃣&nbsp; &nbsp; &nbsp; &nbsp; - &gt; Height = 0 for all nodes at this level</p>

3

Talks at Events (7) Attended Events (8)
View More
Communities (13)

Women Who Code Delhi

5922 members

GDG New Delhi

55394 members

GDG Cloud New Delhi

49001 members

WTM Delhi

19013 members

Women in Machine Learning and Data Science, Delhi

3914 members

CodeChef TPGIT

736 members

View More (7)

Cookies

This website uses cookies to improve your online experience. By continuing to use this website, you agree to our use of cookies. If you would like to, you can change your cookie settings at any time. Our Privacy Notice provides more information about what cookies we use.