
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> 4️⃣ </p> <p> / \ </p> <p> 3️⃣ 5️⃣</p> <p> / \ / \</p> <p> 1️⃣ 2️⃣ 6️⃣ 7️⃣ </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. </p> <p>Node with value <span style="background-color: #f8cac6;">4</span> --> called root node coz it has no parent above it</p> <p>Node with value <span style="background-color: #fbeeb8;">3</span> --> 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> --> 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> --> 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> --> 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> --> 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> --> 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 -> 1, 2, 6, 7</li> <li>three parent nodes -> 4, 3, 5</li> <li>one root node -> 4</li> <li>one tree -> 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
<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> </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> </p> <p>Now, let's see the height:</p> <p> 4️⃣ - > Height = 2 for all nodes at this level</p> <p> / \ </p> <p> 3️⃣ 5️⃣ - > Height = 1 for all nodes at this level</p> <p> / \ / \</p> <p> 1️⃣ 2️⃣ 6️⃣ 7️⃣ - > Height = 0 for all nodes at this level</p>
3

Women Who Code Delhi
5922 members
.png)
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


