Note that there are some explanatory texts on larger screens.

plurals
  1. POin C++ ,what is this for? Node* &head,
    primarykey
    data
    text
    <p>I have a question regarding this code piece. <code>Node* &amp;head</code>, in insert() function, I have hard time understanding meaning of it. Is this "the reference of a pointer poniting to Node structure" ?</p> <p>But why in here <code>getDepth(Node *head)</code> he uses <code>Node *head</code> ? Is this a good practice? what's the better way to write it if possible?</p> <p>Thank you!</p> <pre><code>#include &lt;iostream&gt; #include &lt;cstring&gt; #include &lt;cmath&gt; using namespace std; const int maxn = 100; struct Node{ int key; Node *lchild, *rchild, *parent; }; Node *head, *p, node[maxn]; int cnt; void init(){ head = p = NULL; memset(node, '\0', sizeof(node)); cnt = 0; } void insert(Node* &amp;head, int x){ if(head == NULL){ node[cnt].key = x; node[cnt].parent = p; head = &amp;node[cnt++]; return; } p = head; if(x &lt; head-&gt;key) insert(head-&gt;lchild, x); else insert(head-&gt;rchild, x); } int d = 0, num = 0, dep[maxn]; void getDepth(Node *head){ if(head == NULL) return; ++d; getDepth(head-&gt;lchild); if(head-&gt;lchild == NULL &amp;&amp; head-&gt;rchild == NULL) dep[num++] = d; getDepth(head-&gt;rchild); --d; } bool isBalance(Node *head){ if(head == NULL) return true; getDepth(head); int max = dep[0], min = dep[0]; for(int i=0; i&lt;num; ++i){ if(dep[i]&gt;max) max = dep[i]; if(dep[i]&lt;min) min = dep[i]; } if(max-min &gt; 1) return false; else return true; } int main(){ init(); int a[] = { 5, 3, 8, 1, 4, 7, 10, 2, 6, 9, 11, 12 }; for(int i=0; i&lt;12; ++i) insert(head, a[i]); cout&lt;&lt;isBalance(head)&lt;&lt;endl; return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload