PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由

<pre class="code"><span style="font-family: %value; font-size: 14px;">03-树1. List Leaves (25)
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (&lt;=10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N-1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a "-" will be put at the position. Any pair of children are separated by a space.

Output Specification:

For each test case, print in one line all the leaves‘ indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

Sample Input:
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
Sample Output:
4 1 5
</span></pre>
<pre class="code"><span style="font-family: %value; font-size: 14px;"><span style="color: #008000;">/*</span><span style="color: #008000;">
思路:需要动态的建立二叉树,然后层次遍历输出叶结点,但是有一个问题是要防止指针丢失,因此我在程序中用一个数组维护各个结点的指针;最后树建完以后,还要想办法找到数根,我采取的措施是:遍历维护结点指针的数组,对其左、右结点值求和,这样除了根结点以外,所有结点都被计入总和中了,利用这一信息便可以找到根节点的位置。
</span><span style="color: #008000;">*/</span><span style="color: #000000;">
#include</span>&lt;stdio.h&gt;<span style="color: #000000;">
#include</span>&lt;<span style="color: #0000ff;">string</span>.h&gt;<span style="color: #000000;">
#include</span>&lt;<span style="color: #0000ff;">malloc</span>.h&gt;
<span style="color: #0000ff;">#define</span> MAXN 10<span style="color: #000000;">

typedef </span><span style="color: #0000ff;">struct</span><span style="color: #000000;"> tnode
{
</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> data;
</span><span style="color: #0000ff;">struct</span> tnode *<span style="color: #000000;"> left;
</span><span style="color: #0000ff;">struct</span> tnode *<span style="color: #000000;"> right;
}TNODE, </span>*<span style="color: #000000;">PTNODE;

typedef </span><span style="color: #0000ff;">struct</span><span style="color: #000000;"> qnode
{
PTNODE data;
</span><span style="color: #0000ff;">struct</span> qnode *<span style="color: #000000;"> next;
}QNODE, </span>*<span style="color: #000000;">PQNODE;

typedef </span><span style="color: #0000ff;">struct</span><span style="color: #000000;"> queue
{
PQNODE front;
PQNODE rear;
}QUEUE, </span>*<span style="color: #000000;">PQUEUE;

PQUEUE MakeQueue();
</span><span style="color: #0000ff;">void</span><span style="color: #000000;"> Add(PQUEUE, PTNODE pTNode);
PTNODE Delete(PQUEUE pQ);
</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> IsEmpty(PQUEUE pQ);

PTNODE arr[MAXN</span>+<span style="color: #800080;">1</span><span style="color: #000000;">];
</span><span style="color: #0000ff;">void</span> Dispose(<span style="color: #0000ff;">int</span> i, <span style="color: #0000ff;">char</span> left, <span style="color: #0000ff;">char</span><span style="color: #000000;"> right);
PTNODE FindRoot(</span><span style="color: #0000ff;">int</span> n); <span style="color: #008000;">//</span><span style="color: #008000;"> n个节点中找根节点</span>

<span style="color: #0000ff;">int</span> main(<span style="color: #0000ff;">void</span><span style="color: #000000;">)
{
</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n, i, isFirst;
</span><span style="color: #0000ff;">char</span><span style="color: #000000;"> left, right;
PTNODE root, head;

scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, &amp;<span style="color: #000000;">n);
memset(arr, </span><span style="color: #800080;">0</span>, <span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(arr));
</span><span style="color: #0000ff;">for</span>(i = <span style="color: #800080;">0</span>; i &lt; n; i++<span style="color: #000000;">)
{
scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;"> %c %c</span><span style="color: #800000;">"</span>, &amp;left, &amp;<span style="color: #000000;">right);
Dispose(i, left, right);
}

root </span>=<span style="color: #000000;"> FindRoot(n);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> printf("%d\n", root-&gt;data); </span><span style="color: #008000;">//</span><span style="color: #008000;"> test
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 层次遍历</span>
PQUEUE pQ =<span style="color: #000000;"> MakeQueue();
Add(pQ, root);
isFirst </span>= <span style="color: #800080;">1</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">while</span>(!<span style="color: #000000;">IsEmpty(pQ))
{
head </span>=<span style="color: #000000;"> Delete(pQ);
</span><span style="color: #0000ff;">if</span>(head-&gt;left == NULL &amp;&amp; head-&gt;right ==<span style="color: #000000;"> NULL)
{
</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(isFirst)
isFirst </span>= <span style="color: #800080;">0</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">else</span><span style="color: #000000;">
putchar(</span><span style="color: #800000;">‘</span> <span style="color: #800000;">‘</span><span style="color: #000000;">);

printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, head-&gt;<span style="color: #000000;">data);
}

Add(pQ, head</span>-&gt;<span style="color: #000000;">left);
Add(pQ, head</span>-&gt;<span style="color: #000000;">right);
}

</span><span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">;
}

</span><span style="color: #0000ff;">void</span> Dispose(<span style="color: #0000ff;">int</span> i, <span style="color: #0000ff;">char</span> left, <span style="color: #0000ff;">char</span> right) <span style="color: #008000;">//</span><span style="color: #008000;"> 当前节点,左、右节点哪个没有建哪个</span>
<span style="color: #000000;">{
</span><span style="color: #0000ff;">if</span>(arr[i] ==<span style="color: #000000;"> NULL)
arr[i] </span>= (PTNODE)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(TNODE));
arr[i]</span>-&gt;data =<span style="color: #000000;"> i;

</span><span style="color: #0000ff;">if</span>(left == <span style="color: #800000;">‘</span><span style="color: #800000;">-</span><span style="color: #800000;">‘</span><span style="color: #000000;">)
arr[i]</span>-&gt;left =<span style="color: #000000;"> NULL;
</span><span style="color: #0000ff;">else</span><span style="color: #000000;">
{
</span><span style="color: #0000ff;">if</span>(arr[left-<span style="color: #800000;">‘</span><span style="color: #800000;">0</span><span style="color: #800000;">‘</span>] ==<span style="color: #000000;"> NULL)
arr[left</span>-<span style="color: #800000;">‘</span><span style="color: #800000;">0</span><span style="color: #800000;">‘</span>] = (PTNODE)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(TNODE));
arr[i]</span>-&gt;left = arr[left-<span style="color: #800000;">‘</span><span style="color: #800000;">0</span><span style="color: #800000;">‘</span><span style="color: #000000;">];
}

</span><span style="color: #0000ff;">if</span>(right == <span style="color: #800000;">‘</span><span style="color: #800000;">-</span><span style="color: #800000;">‘</span><span style="color: #000000;">)
arr[i]</span>-&gt;right =<span style="color: #000000;"> NULL;
</span><span style="color: #0000ff;">else</span><span style="color: #000000;">
{
</span><span style="color: #0000ff;">if</span>(arr[right-<span style="color: #800000;">‘</span><span style="color: #800000;">0</span><span style="color: #800000;">‘</span>] ==<span style="color: #000000;"> NULL)
arr[right</span>-<span style="color: #800000;">‘</span><span style="color: #800000;">0</span><span style="color: #800000;">‘</span>] = (PTNODE)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(TNODE));
arr[i]</span>-&gt;right = arr[right-<span style="color: #800000;">‘</span><span style="color: #800000;">0</span><span style="color: #800000;">‘</span><span style="color: #000000;">];
}

</span><span style="color: #0000ff;">return</span><span style="color: #000000;">;

}

PTNODE FindRoot(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n)
{
</span><span style="color: #0000ff;">int</span> i, sum = <span style="color: #800080;">0</span><span style="color: #000000;">;

</span><span style="color: #0000ff;">for</span>(i = <span style="color: #800080;">0</span>; i &lt; n; i++<span style="color: #000000;">)
{
</span><span style="color: #0000ff;">if</span>(arr[i]-&gt;<span style="color: #000000;">left)
sum </span>+= arr[i]-&gt;left-&gt;<span style="color: #000000;">data;
</span><span style="color: #0000ff;">if</span>(arr[i]-&gt;<span style="color: #000000;">right)
sum </span>+= arr[i]-&gt;right-&gt;<span style="color: #000000;">data;
}

</span><span style="color: #0000ff;">return</span> arr[n*(n-<span style="color: #800080;">1</span>)/<span style="color: #800080;">2</span> - sum]; <span style="color: #008000;">//</span><span style="color: #008000;"> 这里用了一点技巧</span>
<span style="color: #000000;">}

PQUEUE MakeQueue()
{
PQUEUE pQ </span>= <span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(QUEUE));
pQ</span>-&gt;front = pQ-&gt;rear =<span style="color: #000000;"> NULL;

</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> pQ;
}

</span><span style="color: #0000ff;">void</span><span style="color: #000000;"> Add(PQUEUE pQ, PTNODE pTNode)
{
</span><span style="color: #0000ff;">if</span>(pTNode ==<span style="color: #000000;"> NULL)
</span><span style="color: #0000ff;">return</span><span style="color: #000000;">;

PQNODE p </span>= (PQNODE)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span>(QNODE)); <span style="color: #008000;">//</span><span style="color: #008000;"> 注意正规的程序应该检查申请堆内存是否成功</span>
p-&gt;data =<span style="color: #000000;"> pTNode;
p</span>-&gt;next =<span style="color: #000000;"> NULL;

</span><span style="color: #0000ff;">if</span>(pQ-&gt;front ==<span style="color: #000000;"> NULL)
pQ</span>-&gt;front = pQ-&gt;rear =<span style="color: #000000;"> p;
</span><span style="color: #0000ff;">else</span><span style="color: #000000;">
{
pQ</span>-&gt;rear-&gt;next =<span style="color: #000000;"> p;
pQ</span>-&gt;rear =<span style="color: #000000;"> p;
}
}

PTNODE Delete(PQUEUE pQ)
{
PTNODE p;
PQNODE pTemp;

</span><span style="color: #0000ff;">if</span>(pQ-&gt;front ==<span style="color: #000000;"> NULL)
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> NULL;
</span><span style="color: #0000ff;">if</span>(pQ-&gt;rear == pQ-&gt;<span style="color: #000000;">front)
pQ</span>-&gt;rear =<span style="color: #000000;"> NULL;

p </span>= pQ-&gt;front-&gt;<span style="color: #000000;">data;

pTemp </span>= pQ-&gt;<span style="color: #000000;">front;
pQ</span>-&gt;front = pQ-&gt;front-&gt;<span style="color: #000000;">next;
</span><span style="color: #0000ff;">free</span><span style="color: #000000;">(pTemp);

</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> p;
}

</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> IsEmpty(PQUEUE pQ)
{
</span><span style="color: #0000ff;">return</span> pQ-&gt;front == NULL ? <span style="color: #800080;">1</span> : <span style="color: #800080;">0</span><span style="color: #000000;">;
}</span></span></pre>
<p>&nbsp;</p>
<pre class="code"><span style="font-family: %value; font-size: 14px;">03-树2. Tree Traversals Again (25)
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.

</span></pre>
<p><img src="http://images0.cnblogs.com/blog2015/672107/201506/201900488733317.png" /></p>
<pre class="code"><span style="font-family: %value; font-size: 14px;">Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (&lt;=30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: "Push X" where X is the index of the node being pushed onto the stack; or "Pop" meaning to pop one node from the stack.

Output Specification:

For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:
6
Push 1
Push 2
Push 3
Pop
Pop
Push 4
Pop
Pop
Push 5
Push 6
Pop
Pop
Sample Output:
3 4 2 6 5 1
</span></pre>
<pre class="code"><span style="font-family: %value; font-size: 14px;"><span style="color: #008000;">/*</span><span style="color: #008000;">
思路:先用一个真实的栈模拟输入,据此中序建树,然后执行后序遍历输出结果即可
</span><span style="color: #008000;">*/</span><span style="color: #000000;">
#include</span>&lt;stdio.h&gt;<span style="color: #000000;">
#include</span>&lt;<span style="color: #0000ff;">malloc</span>.h&gt;<span style="color: #000000;">
typedef </span><span style="color: #0000ff;">struct</span><span style="color: #000000;"> tnode
{
</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> data;
</span><span style="color: #0000ff;">struct</span> tnode *<span style="color: #000000;"> left;
</span><span style="color: #0000ff;">struct</span> tnode *<span style="color: #000000;"> right;
}TNODE, </span>*<span style="color: #000000;">PTNODE;

typedef </span><span style="color: #0000ff;">struct</span><span style="color: #000000;"> snode
{
PTNODE data;
</span><span style="color: #0000ff;">struct</span> snode *<span style="color: #000000;"> next;
}SNODE, </span>*<span style="color: #000000;">PSNODE;

PSNODE MakeStack();
</span><span style="color: #0000ff;">void</span> Push(PSNODE *<span style="color: #000000;"> pS, PTNODE data);
PTNODE Pop(PSNODE </span>*<span style="color: #000000;"> pS);
</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> IsEmpty(PSNODE pS);

PTNODE Dispose(PTNODE </span>* lastPTNode, <span style="color: #0000ff;">int</span><span style="color: #000000;"> numberNode);
</span><span style="color: #0000ff;">void</span><span style="color: #000000;"> PostOrderTraverse(PTNODE pTNode);

</span><span style="color: #0000ff;">int</span> first = <span style="color: #800080;">1</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> 后序遍历中是否是第一个输出节点的标记</span>

<span style="color: #0000ff;">int</span> main(<span style="color: #0000ff;">void</span><span style="color: #000000;">)
{
</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n, numberNode;
PTNODE root, lastPTNode, currentPTNode;
PSNODE pS;
</span><span style="color: #0000ff;">char</span> str[<span style="color: #800080;">10</span><span style="color: #000000;">];

pS </span>=<span style="color: #000000;"> MakeStack();

scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, &amp;<span style="color: #000000;">n);
scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%s%d</span><span style="color: #800000;">"</span>, str, &amp;<span style="color: #000000;">numberNode);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 建立数根root,并将之压栈</span>
root = lastPTNode = (PTNODE)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(TNODE));
root</span>-&gt;data =<span style="color: #000000;"> numberNode;
root</span>-&gt;left = root-&gt;right =<span style="color: #000000;"> NULL;
Push(</span>&amp;<span style="color: #000000;">pS, root);

</span><span style="color: #0000ff;">while</span>(scanf(<span style="color: #800000;">"</span><span style="color: #800000;">%s</span><span style="color: #800000;">"</span>, str) !=<span style="color: #000000;"> EOF)
{
</span><span style="color: #0000ff;">if</span>(str[<span style="color: #800080;">1</span>] == <span style="color: #800000;">‘</span><span style="color: #800000;">u</span><span style="color: #800000;">‘</span><span style="color: #000000;">)
{
scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, &amp;<span style="color: #000000;">numberNode);
currentPTNode </span>= Dispose(&amp;<span style="color: #000000;">lastPTNode, numberNode);
Push(</span>&amp;<span style="color: #000000;">pS, currentPTNode);
}
</span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(str[<span style="color: #800080;">1</span>] == <span style="color: #800000;">‘</span><span style="color: #800000;">o</span><span style="color: #800000;">‘</span><span style="color: #000000;">)
{
lastPTNode </span>= Pop(&amp;<span style="color: #000000;">pS);
}
}

PostOrderTraverse(root);

</span><span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">;
}

PSNODE MakeStack()
{
PSNODE p </span>= (PSNODE)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(SNODE));
p</span>-&gt;data =<span style="color: #000000;"> NULL;

</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> p;
}
</span><span style="color: #0000ff;">void</span> Push(PSNODE *<span style="color: #000000;"> pS, PTNODE data)
{
PSNODE p </span>= (PSNODE)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(SNODE));

p</span>-&gt;data =<span style="color: #000000;"> data;
p</span>-&gt;next = *<span style="color: #000000;">pS;
</span>*pS =<span style="color: #000000;"> p;
}
PTNODE Pop(PSNODE </span>*<span style="color: #000000;"> pS)
{
PTNODE data;
PSNODE pTemp;
</span><span style="color: #0000ff;">if</span>(IsEmpty(*<span style="color: #000000;">pS))
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> NULL;

data </span>= (*pS)-&gt;data; <span style="color: #008000;">//</span><span style="color: #008000;"> 优先级!</span>
pTemp = *<span style="color: #000000;">pS;
</span>*pS = (*pS)-&gt;<span style="color: #000000;">next;
</span><span style="color: #0000ff;">free</span><span style="color: #000000;">(pTemp);

</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> data;
}

</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> IsEmpty(PSNODE pS)
{
</span><span style="color: #0000ff;">return</span> pS == NULL ? <span style="color: #800080;">1</span> : <span style="color: #800080;">0</span><span style="color: #000000;">;
}

PTNODE Dispose(PTNODE </span>* lastPTNode, <span style="color: #0000ff;">int</span><span style="color: #000000;"> numberNode)
{
PTNODE pTemp;

pTemp </span>= (PTNODE)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(TNODE));
pTemp</span>-&gt;data =<span style="color: #000000;"> numberNode;
pTemp</span>-&gt;left = pTemp-&gt;right =<span style="color: #000000;"> NULL;
</span><span style="color: #0000ff;">if</span>((*lastPTNode)-&gt;left == NULL) <span style="color: #008000;">//</span><span style="color: #008000;"> 注意运算符优先级</span>
(*lastPTNode)-&gt;left =<span style="color: #000000;"> pTemp;
</span><span style="color: #0000ff;">else</span><span style="color: #000000;">
(</span>*lastPTNode)-&gt;right =<span style="color: #000000;"> pTemp;

</span>*lastPTNode =<span style="color: #000000;"> pTemp;

</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> pTemp;
}

</span><span style="color: #0000ff;">void</span><span style="color: #000000;"> PostOrderTraverse(PTNODE pTNode)
{
</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(pTNode)
{

PostOrderTraverse(pTNode</span>-&gt;<span style="color: #000000;">left);
PostOrderTraverse(pTNode</span>-&gt;<span style="color: #000000;">right);
</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(first)
first </span>= <span style="color: #800080;">0</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">else</span><span style="color: #000000;">
putchar(</span><span style="color: #800000;">‘</span> <span style="color: #800000;">‘</span><span style="color: #000000;">);
printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, pTNode-&gt;<span style="color: #000000;">data);
}

</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> ;
}</span></span></pre>
<pre class="code"><span style="font-family: %value; font-size: 14px;">04-树3. Root of AVL Tree (25)
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.

</span></pre>
<p><img src="http://images0.cnblogs.com/blog2015/672107/201506/201927126709124.png" /></p>
<pre class="code"><span style="font-family: %value; font-size: 14px;">

Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL tree.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (&lt;=20) which is the total number of keys to be inserted. Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print ythe root of the resulting AVL tree in one line.

Sample Input 1:
5
88 70 61 96 120
Sample Output 1:
70
Sample Input 2:
7
88 70 61 96 120 90 65
Sample Output 2:
88
</span></pre>
<pre class="code"><span style="font-family: %value; font-size: 14px;">// 很直白的考察AVL建树,不过多赘述了~<br />#include&lt;stdio.h&gt;<span style="color: #000000;">
#include</span>&lt;<span style="color: #0000ff;">malloc</span>.h&gt;<span style="color: #000000;">
typedef </span><span style="color: #0000ff;">struct</span><span style="color: #000000;"> node
{
</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> data;
</span><span style="color: #0000ff;">struct</span> node *<span style="color: #000000;"> left;
</span><span style="color: #0000ff;">struct</span> node *<span style="color: #000000;"> right;
</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> height;
}NODE, </span>*<span style="color: #000000;">PNODE;
PNODE Insert(PNODE pNode, </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> data);
</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> Height(PNODE pNode);
</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> MaxHeight(PNODE pLeft, PNODE pRight);
PNODE SingleRightRotation(PNODE A);
PNODE DoubleRightLeftRotation(PNODE A);
PNODE SingleLeftRotation(PNODE A);
PNODE DoubleLeftRightRotation(PNODE A);
</span><span style="color: #0000ff;">int</span> main(<span style="color: #0000ff;">void</span><span style="color: #000000;">)
{
</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n, data;
PNODE avlRoot;

scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, &amp;<span style="color: #000000;">n);
scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, &amp;<span style="color: #000000;">data);
avlRoot </span>=<span style="color: #000000;"> Insert(NULL, data);
n</span>--<span style="color: #000000;">;
</span><span style="color: #0000ff;">while</span>(n--<span style="color: #000000;">)
{
scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, &amp;<span style="color: #000000;">data);
avlRoot </span>=<span style="color: #000000;"> Insert(avlRoot, data);
}

printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d\n</span><span style="color: #800000;">"</span>, avlRoot-&gt;<span style="color: #000000;">data);
</span><span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">;
}

PNODE Insert(PNODE pRoot, </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> data)
{
</span><span style="color: #0000ff;">if</span>(pRoot ==<span style="color: #000000;"> NULL)
{
pRoot </span>= (PNODE)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(NODE));
pRoot</span>-&gt;data =<span style="color: #000000;"> data;
pRoot</span>-&gt;left = pRoot-&gt;right =<span style="color: #000000;"> NULL;
pRoot</span>-&gt;height = <span style="color: #800080;">0</span><span style="color: #000000;">;
}
</span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(data &gt; pRoot-&gt;<span style="color: #000000;">data)
{
pRoot</span>-&gt;right = Insert(pRoot-&gt;<span style="color: #000000;">right, data);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 判断是否要旋转</span>
<span style="color: #0000ff;">if</span>(Height(pRoot-&gt;right) - Height(pRoot-&gt;left) == <span style="color: #800080;">2</span><span style="color: #000000;">)
{
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 判断是右右旋转还是右左旋转</span>
<span style="color: #0000ff;">if</span>(data &gt; pRoot-&gt;right-&gt;data) <span style="color: #008000;">//</span><span style="color: #008000;"> 右右旋转</span>
<span style="color: #0000ff;">return</span> SingleRightRotation(pRoot); <span style="color: #008000;">//</span><span style="color: #008000;"> 旋转完了返回修正部分的根节点,并将其返回给上一次调用它的父节点</span>
<span style="color: #0000ff;">else</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 右左旋转</span>
<span style="color: #0000ff;">return</span><span style="color: #000000;"> DoubleRightLeftRotation(pRoot);
}

}
</span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(data &lt; pRoot-&gt;<span style="color: #000000;">data)
{
pRoot</span>-&gt;left = Insert(pRoot-&gt;<span style="color: #000000;">left, data);
</span><span style="color: #0000ff;">if</span>(Height(pRoot-&gt;left) - Height(pRoot-&gt;right) == <span style="color: #800080;">2</span><span style="color: #000000;">)
{
</span><span style="color: #0000ff;">if</span>(data &lt; pRoot-&gt;left-&gt;data) <span style="color: #008000;">//</span><span style="color: #008000;"> 左左旋转</span>
<span style="color: #0000ff;">return</span><span style="color: #000000;"> SingleLeftRotation(pRoot);
</span><span style="color: #0000ff;">else</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 左右旋转</span>
<span style="color: #0000ff;">return</span><span style="color: #000000;"> DoubleLeftRightRotation(pRoot);

}
}

pRoot</span>-&gt;height = MaxHeight(pRoot-&gt;left, pRoot-&gt;right) + <span style="color: #800080;">1</span><span style="color: #000000;">;

</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> pRoot;
}

</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> Height(PNODE pNode)
{
</span><span style="color: #0000ff;">if</span>(pNode ==<span style="color: #000000;"> NULL)
</span><span style="color: #0000ff;">return</span> -<span style="color: #800080;">1</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">else</span>
<span style="color: #0000ff;">return</span> pNode-&gt;<span style="color: #000000;">height;
}

</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> MaxHeight(PNODE pLeft, PNODE pRight)
{
</span><span style="color: #0000ff;">if</span>(pLeft == NULL &amp;&amp; pRight ==<span style="color: #000000;"> NULL)
</span><span style="color: #0000ff;">return</span> -<span style="color: #800080;">1</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(pLeft ==<span style="color: #000000;"> NULL)
</span><span style="color: #0000ff;">return</span> pRight-&gt;<span style="color: #000000;">height;
</span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(pRight ==<span style="color: #000000;"> NULL)
</span><span style="color: #0000ff;">return</span> pLeft-&gt;<span style="color: #000000;">height;
</span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(pLeft-&gt;height &gt; pRight-&gt;<span style="color: #000000;">height)
</span><span style="color: #0000ff;">return</span> pLeft-&gt;<span style="color: #000000;">height;
</span><span style="color: #0000ff;">else</span>
<span style="color: #0000ff;">return</span> pRight-&gt;<span style="color: #000000;">height;
}

PNODE SingleRightRotation(PNODE A) </span><span style="color: #008000;">//</span><span style="color: #008000;"> 右右旋转,返回旋转部分的根节点</span>
<span style="color: #000000;">{
PNODE B </span>= A-&gt;<span style="color: #000000;">right;
A</span>-&gt;right = B-&gt;<span style="color: #000000;">left;
B</span>-&gt;left =<span style="color: #000000;"> A;

</span><span style="color: #008000;">/*</span><span style="color: #008000;"> 可以代替下面两行:
A-&gt;height = Height(A-&gt;left) + 1; // 这里没有通过比较更新A的高度,因为我觉得其高度不会变,因为BL不会大于AL(否则在之前它们就不平衡了),更具体的原因可以在我之前的博文"AVL树旋转特性"中找到
B-&gt;height = A-&gt;height + 1; // 与上述理由类似
</span><span style="color: #008000;">*/</span><span style="color: #000000;">

A</span>-&gt;height = MaxHeight(A-&gt;left, A-&gt;right) + <span style="color: #800080;">1</span><span style="color: #000000;">;
B</span>-&gt;height = MaxHeight(B-&gt;left, A) + <span style="color: #800080;">1</span><span style="color: #000000;">;

</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> B;
}

PNODE DoubleRightLeftRotation(PNODE A)
{
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 注意到只需要左左旋转然后右右旋转,两者组合起来就是右左旋转</span>
A-&gt;right = SingleLeftRotation(A-&gt;right); <span style="color: #008000;">//</span><span style="color: #008000;"> 以B为根进行左左旋转</span>
<span style="color: #0000ff;">return</span> SingleRightRotation(A); <span style="color: #008000;">//</span><span style="color: #008000;"> 以A为根进行右右旋转</span>
<span style="color: #000000;">}

PNODE SingleLeftRotation(PNODE A)
{
PNODE B </span>= A-&gt;<span style="color: #000000;">left;
A</span>-&gt;left = B-&gt;<span style="color: #000000;">right;
B</span>-&gt;right =<span style="color: #000000;"> A;

A</span>-&gt;height = MaxHeight(A-&gt;left, A-&gt;right) + <span style="color: #800080;">1</span><span style="color: #000000;">;
B</span>-&gt;height = MaxHeight(B-&gt;left, A) + <span style="color: #800080;">1</span><span style="color: #000000;">;

</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> B;
}

PNODE DoubleLeftRightRotation(PNODE A)
{
A</span>-&gt;left = SingleRightRotation(A-&gt;<span style="color: #000000;">left);
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> SingleLeftRotation(A);
}</span></span></pre>
<p>&nbsp;</p>
<pre class="code"><span style="font-family: %value; font-size: 14px;">04-树4. Search in a Binary Search Tree (25)
To search a key in a binary search tree, we start from the root and move all the way down, choosing branches according to the comparison results of the keys. The searching path corresponds to a sequence of keys. For example, following {1, 4, 2, 3} we can find 3 from a binary search tree with 1 as its root. But {2, 4, 1, 3} is not such a path since 1 is in the right subtree of the root 2, which breaks the rule for a binary search tree. Now given a sequence of keys, you are supposed to tell whether or not it indeed correspnds to a searching path in a binary search tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N and M (&lt;=100) which are the total number of sequences, and the size of each sequence, respectively. Then N lines follow, each gives a sequence of keys. It is assumed that the keys are numbered from 1 to M.

Output Specification:

For each sequence, print in a line "YES" if the sequence does correspnd to a searching path in a binary search tree, or "NO" if not.

Sample Input:
3 4
1 4 2 3
2 4 1 3
3 2 4 1
Sample Output:
YES
NO
NO
</span></pre>
<pre class="code"><span style="font-family: %value; font-size: 14px;">/*
思路:按照给定的数据建树,只要当前结点是上一个节点的孩子,则继续这个过程,如果到达输入末尾,则说明路径存在,否则路径不存在,PS:由于这个思路执行起来更快,所以选择了它,其实解决这个问题的时候,我首先想到的办法是:根据输入建树,然后最后输入的那个结点,如果查找的过程中经过了所有结点,那么路径存在,否则路径不存在
*/
#include&lt;stdio.h&gt;
#include&lt;malloc.h&gt;
typedef struct node
{
int data;
struct node * left;
struct node * right;
}NODE, *PNODE;

PNODE Insert(PNODE root, int data); // 返回插入的节点,如果节点已经存在,返回空指针
PNODE Find(PNODE root, int data);
void Delete(PNODE root);
int main(void)
{
int flag; // 当前节点是否是上一个节点的孩子的标记变量
PNODE curNode, preNode, root;
int i, n, m, data;

scanf("%d%d", &amp;n, &amp;m);
root = NULL;
while(n--)
{
Delete(root); // 删除上一次建的树
scanf("%d", &amp;data);
root = NULL; // 注意一定要置空,否则用上次释放的root做根会出现访问非法的问题
root = Insert(root, data); // 首先建立根节点
preNode = root;
flag = 1;
for(i = 1; i &lt; m; i++)
{
scanf("%d", &amp;data);
if(flag)
{
Insert(root, data);
curNode = Find(root, data);
if(preNode-&gt;left != curNode &amp;&amp; preNode-&gt;right != curNode)
flag = 0;

preNode = curNode;
}
}
if(flag)
printf("YES\n");
else
printf("NO\n");
}

return 0;
}

PNODE Insert(PNODE root, int data)
{
if(root == NULL)
{
PNODE pNode = (PNODE)malloc(sizeof(NODE));
pNode-&gt;data = data;
pNode-&gt;left = pNode-&gt;right = NULL;
return pNode;
}

if(data &gt; root-&gt;data)
root-&gt;right = Insert(root-&gt;right, data); // 由于插入的时候需要记住它的父节点,因此这里有赋值语句
else if(data &lt; root-&gt;data)
root-&gt;left = Insert(root-&gt;left, data);
else // 数据相等的话直接退出,因为二叉搜索树不允许有相同的键值
return NULL;

return root;
}

PNODE Find(PNODE root, int data)
{
if(root == NULL)
return NULL;
if(data &gt; root-&gt;data)
return Find(root-&gt;right, data);
else if(data &lt; root-&gt;data)
return Find(root-&gt;left, data);
else
return root;
}

void Delete(PNODE root)
{
if(root == NULL)
return ;

Delete(root-&gt;right); // 删除右子树
root-&gt;right = NULL; // 注意到这里需要赋值语句用来修改节点的右指针

Delete(root-&gt;left);
root-&gt;left = NULL;

free(root); // 释放根节点的空间
}
</span></pre>
<pre class="code"><span style="font-family: %value; font-size: 14px;">04-树5. Complete Binary Search Tree (30)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

The left subtree of a node contains only nodes with keys less than the node‘s key.
The right subtree of a node contains only nodes with keys greater than or equal to the node‘s key.
Both the left and right subtrees must also be binary search trees.
A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right.

Now given a sequence of distinct non-negative integer keys, a unique BST can be constructed if it is required that the tree must also be a CBT. You are supposed to output the level order traversal sequence of this BST.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (&lt;=1000). Then N distinct non-negative integer keys are given in the next line. All the numbers in a line are separated by a space and are no greater than 2000.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding complete binary search tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

Sample Input:
10
1 2 3 4 5 6 7 8 9 0
Sample Output:
6 3 8 1 5 7 9 0 2 4
</span></pre>
<pre class="code"><span style="font-family: %value; font-size: 14px;"><span style="color: #008000;">/*</span><span style="color: #008000;">
首先,这个题真的很有意思:~
思路: 把输入数据放入数组冒泡排序
用类似层次遍历的方式建立一颗有N个节点的完全二叉树,每次malloc一个节点num加一
采用中序遍历的方式用一个全局变量将所用的节点标号
层次遍历,根据标号(对应数组下标)放入数值(为了更好的达到练习效果,我直接先序了),同时输出
</span><span style="color: #008000;">*/</span><span style="color: #000000;">
#include</span>&lt;stdio.h&gt;<span style="color: #000000;">
#include</span>&lt;<span style="color: #0000ff;">malloc</span>.h&gt;
<span style="color: #0000ff;">#define</span> MAXN 1000
<span style="color: #0000ff;">int</span> Arr[MAXN + <span style="color: #800080;">10</span><span style="color: #000000;">];
</span><span style="color: #0000ff;">int</span> G_NUM; <span style="color: #008000;">//</span><span style="color: #008000;"> 定义成全局变量方便创建完全二叉树</span>
<span style="color: #0000ff;">int</span> G_COUNT = <span style="color: #800080;">0</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> 记录已经创建的树的节点的个数</span>
<span style="color: #0000ff;">int</span> G_NUMBER = <span style="color: #800080;">0</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> 节点标号,左-中-右依次递增 </span>
<span style="color: #0000ff;">void</span> BbSort(<span style="color: #0000ff;">int</span> Arr[], <span style="color: #0000ff;">int</span><span style="color: #000000;"> n);

typedef </span><span style="color: #0000ff;">struct</span><span style="color: #000000;"> TNode
{
</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> data;
</span><span style="color: #0000ff;">struct</span> TNode *<span style="color: #000000;"> left;
</span><span style="color: #0000ff;">struct</span> TNode *<span style="color: #000000;"> right;
}TNODE, </span>*<span style="color: #000000;">PTNODE;

typedef </span><span style="color: #0000ff;">struct</span><span style="color: #000000;"> QNode
{
PTNODE data;
</span><span style="color: #0000ff;">struct</span> QNode *<span style="color: #000000;"> next;
}QNODE, </span>*<span style="color: #000000;">PQNODE;
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 树操作</span>
<span style="color: #000000;">PTNODE CreateCBT();
</span><span style="color: #0000ff;">void</span> LevelOrderTraversal(PTNODE pRoot); <span style="color: #008000;">//</span><span style="color: #008000;"> 用层次遍历的方式建立完全二叉树</span>
<span style="color: #0000ff;">void</span> InOrderTraversal(PTNODE pRoot); <span style="color: #008000;">//</span><span style="color: #008000;"> 用中序遍历的方式给节点编号</span>
<span style="color: #0000ff;">void</span> PreOrderTraversal(PTNODE pRoot); <span style="color: #008000;">//</span><span style="color: #008000;"> 用先序遍历的方式给节点赋值</span>
<span style="color: #0000ff;">void</span> LevelOrderTraversal2(PTNODE pRoot); <span style="color: #008000;">//</span><span style="color: #008000;"> 输出层次遍历结果</span>
typedef <span style="color: #0000ff;">struct</span><span style="color: #000000;"> Queue
{
PQNODE front;
PQNODE rear;
}QUEUE, </span>*<span style="color: #000000;">PQUEUE;

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 队列操作</span>
<span style="color: #000000;">PQUEUE CreatQueue();
</span><span style="color: #0000ff;">void</span><span style="color: #000000;"> EnQueue(PQUEUE pQueue, PTNODE pTNode);
PTNODE DeQueue(PQUEUE pQueue);
</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> IsEmpty(PQUEUE pQueue);

</span><span style="color: #0000ff;">int</span> main(<span style="color: #0000ff;">void</span><span style="color: #000000;">)
{
</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i;
PTNODE pRoot;

scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, &amp;<span style="color: #000000;">G_NUM);
</span><span style="color: #0000ff;">for</span>(i = <span style="color: #800080;">0</span>; i &lt; G_NUM; i++<span style="color: #000000;">)
scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, &amp;<span style="color: #000000;">Arr[i]);
BbSort(Arr, G_NUM);

pRoot </span>=<span style="color: #000000;"> CreateCBT();
LevelOrderTraversal(pRoot); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 层次遍历的方式建立完全二叉树</span>
<span style="color: #000000;">
InOrderTraversal(pRoot); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 中序遍历标号

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 下面两行代码其实可以用一个层次遍历来代替, 就是给节点赋值的同时输出该值,当然按照AC的角度理解的话,可以不用赋值直接输出</span>
PreOrderTraversal(pRoot); <span style="color: #008000;">//</span><span style="color: #008000;"> 先序遍历给节点赋值</span>
<span style="color: #000000;">
LevelOrderTraversal2(pRoot); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 层次遍历的同时输出节点的值</span>

<span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">;
}

</span><span style="color: #0000ff;">void</span> BbSort(<span style="color: #0000ff;">int</span> Arr[], <span style="color: #0000ff;">int</span><span style="color: #000000;"> n)
{
</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> temp, outer, inner, flag;

flag </span>= <span style="color: #800080;">1</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">for</span>(outer = n-<span style="color: #800080;">1</span>; outer &gt; <span style="color: #800080;">0</span> &amp;&amp; flag; outer--<span style="color: #000000;">)
{
flag </span>= <span style="color: #800080;">0</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">for</span>(inner = <span style="color: #800080;">0</span>; inner &lt; outer; inner++<span style="color: #000000;">)
{
</span><span style="color: #0000ff;">if</span>(Arr[inner] &gt; Arr[inner+<span style="color: #800080;">1</span><span style="color: #000000;">])
{
temp </span>=<span style="color: #000000;"> Arr[inner];
Arr[inner] </span>= Arr[inner+<span style="color: #800080;">1</span><span style="color: #000000;">];
Arr[inner</span>+<span style="color: #800080;">1</span>] =<span style="color: #000000;"> temp;
flag </span>= <span style="color: #800080;">1</span><span style="color: #000000;">;
}
}
}
}

PTNODE CreateCBT()
{
</span><span style="color: #0000ff;">if</span>(G_COUNT ==<span style="color: #000000;"> G_NUM)
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> NULL;

PTNODE root </span>= (PTNODE)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(TNODE));
root</span>-&gt;left = root-&gt;right =<span style="color: #000000;"> NULL;
G_COUNT</span>++<span style="color: #000000;">;
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> root;
}

</span><span style="color: #0000ff;">void</span><span style="color: #000000;"> LevelOrderTraversal(PTNODE pRoot)
{
PTNODE pTNode;
PQUEUE pQueue </span>=<span style="color: #000000;"> CreatQueue();
EnQueue(pQueue, pRoot);
</span><span style="color: #0000ff;">while</span>(!<span style="color: #000000;">IsEmpty(pQueue))
{
pTNode </span>=<span style="color: #000000;"> DeQueue(pQueue);
pTNode</span>-&gt;left =<span style="color: #000000;"> CreateCBT();
pTNode</span>-&gt;right =<span style="color: #000000;"> CreateCBT();
EnQueue(pQueue, pTNode</span>-&gt;<span style="color: #000000;">left);
EnQueue(pQueue, pTNode</span>-&gt;<span style="color: #000000;">right);
}
}

</span><span style="color: #0000ff;">void</span><span style="color: #000000;"> LevelOrderTraversal2(PTNODE pRoot)
{
</span><span style="color: #0000ff;">int</span> flag = <span style="color: #800080;">0</span><span style="color: #000000;">;
PTNODE pTNode;
PQUEUE pQueue </span>=<span style="color: #000000;"> CreatQueue();
EnQueue(pQueue, pRoot);
</span><span style="color: #0000ff;">while</span>(!<span style="color: #000000;">IsEmpty(pQueue))
{
pTNode </span>=<span style="color: #000000;"> DeQueue(pQueue);
</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(flag)
putchar(</span><span style="color: #800000;">‘</span> <span style="color: #800000;">‘</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">else</span><span style="color: #000000;">
flag </span>= <span style="color: #800080;">1</span><span style="color: #000000;">;
printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, pTNode-&gt;<span style="color: #000000;">data);
EnQueue(pQueue, pTNode</span>-&gt;<span style="color: #000000;">left);
EnQueue(pQueue, pTNode</span>-&gt;<span style="color: #000000;">right);
}
}

</span><span style="color: #0000ff;">void</span><span style="color: #000000;"> InOrderTraversal(PTNODE pRoot)
{
</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(pRoot)
{
InOrderTraversal(pRoot</span>-&gt;<span style="color: #000000;">left);
pRoot</span>-&gt;data =<span style="color: #000000;"> G_NUMBER;
G_NUMBER</span>++<span style="color: #000000;">;
InOrderTraversal(pRoot</span>-&gt;<span style="color: #000000;">right);
}
}
</span><span style="color: #0000ff;">void</span><span style="color: #000000;"> PreOrderTraversal(PTNODE pRoot)
{
</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(pRoot)
{
pRoot</span>-&gt;data = Arr[pRoot-&gt;<span style="color: #000000;">data];
PreOrderTraversal(pRoot</span>-&gt;<span style="color: #000000;">left);
PreOrderTraversal(pRoot</span>-&gt;<span style="color: #000000;">right);
}
}
PQUEUE CreatQueue()
{
PQUEUE pQ </span>= (PQUEUE)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(QUEUE));
pQ</span>-&gt;front = pQ-&gt;rear =<span style="color: #000000;"> NULL;
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> pQ;
}

</span><span style="color: #0000ff;">void</span><span style="color: #000000;"> EnQueue(PQUEUE pQueue, PTNODE pTNode)
{
</span><span style="color: #0000ff;">if</span>(pTNode ==<span style="color: #000000;"> NULL)
</span><span style="color: #0000ff;">return</span><span style="color: #000000;">;
PQNODE pQNode</span>= (PQNODE)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(QNODE));
pQNode</span>-&gt;data =<span style="color: #000000;"> pTNode;
pQNode</span>-&gt;next =<span style="color: #000000;"> NULL;

</span><span style="color: #0000ff;">if</span>(pQueue-&gt;front ==<span style="color: #000000;"> NULL)
pQueue</span>-&gt;front = pQueue-&gt;rear =<span style="color: #000000;"> pQNode;
</span><span style="color: #0000ff;">else</span><span style="color: #000000;">
{
pQueue</span>-&gt;rear-&gt;next =<span style="color: #000000;"> pQNode;
pQueue</span>-&gt;rear =<span style="color: #000000;"> pQNode;
}
}

PTNODE DeQueue(PQUEUE pQueue)
{
PTNODE pTNode;

</span><span style="color: #0000ff;">if</span>(pQueue-&gt;front ==<span style="color: #000000;"> NULL)
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> NULL;
</span><span style="color: #0000ff;">if</span>(pQueue-&gt;rear == pQueue-&gt;<span style="color: #000000;">front)
pQueue</span>-&gt;rear =<span style="color: #000000;"> NULL;

PQNODE pQNode </span>= pQueue-&gt;<span style="color: #000000;">front;
pQueue</span>-&gt;front = pQueue-&gt;front-&gt;<span style="color: #000000;">next;
pTNode </span>= pQNode-&gt;<span style="color: #000000;">data;
</span><span style="color: #0000ff;">free</span><span style="color: #000000;">(pQNode);
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> pTNode;
}

</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> IsEmpty(PQUEUE pQueue)
{
</span><span style="color: #0000ff;">if</span>(pQueue-&gt;front ==<span style="color: #000000;"> NULL)
</span><span style="color: #0000ff;">return</span> <span style="color: #800080;">1</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">else</span>
<span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">;
}</span></span></pre>
<p>&nbsp;</p>
<pre class="code"><span style="font-family: %value; font-size: 14px;">05-树6. Path in a Heap (25)
Insert a sequence of given numbers into an initially empty min-heap H. Then for any given index i, you are supposed to print the path from H[i] to the root.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N and M (&lt;=1000) which are the size of the input sequence, and the number of indices to be checked, respectively. Given in the next line are the N integers in [-10000, 10000] which are supposed to be inserted into an initially empty min-heap. Finally in the last line, M indices are given.

Output Specification:

For each index i in the input, print in one line the numbers visited along the path from H[i] to the root of the heap. The numbers are separated by a space, and there must be no extra space at the end of the line.

Sample Input:
5 3
46 23 26 24 10
5 4 3
Sample Output:
24 23 10
46 23 10
26 10
</span></pre>
<pre class="code"><span style="font-family: %value; font-size: 14px;">#include&lt;stdio.h&gt; <span style="color: #008000;">//</span><span style="color: #008000;"> 堆常见的操作有Creat、Insert、Delete、IsFull、IsEmpty这里只关心AC,因此省略了不相干操作的实现</span>
#include&lt;<span style="color: #0000ff;">malloc</span>.h&gt;
<span style="color: #0000ff;">#define</span> CAPACITY 1000 <span style="color: #008000;">//</span><span style="color: #008000;"> 堆的最大容量</span>
<span style="color: #0000ff;">#define</span> MIN -10001 <span style="color: #008000;">//</span><span style="color: #008000;"> 堆数组中下标为0的元素-哨兵的应赋予的初值</span><span style="color: #000000;">
typedef </span><span style="color: #0000ff;">struct</span><span style="color: #000000;"> Heap
{
</span><span style="color: #0000ff;">int</span> *<span style="color: #000000;"> arr;
</span><span style="color: #0000ff;">int</span> size; <span style="color: #008000;">//</span><span style="color: #008000;"> 当前元素的个数</span>
<span style="color: #0000ff;">int</span> capacity; <span style="color: #008000;">//</span><span style="color: #008000;"> 最大容量</span>
}HEAP, *<span style="color: #000000;">PHEAP;
PHEAP CreateHeap(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> capacity);
</span><span style="color: #0000ff;">void</span> Insert(PHEAP pHeap, <span style="color: #0000ff;">int</span><span style="color: #000000;"> data);
</span><span style="color: #0000ff;">void</span> PrintPath(PHEAP pHeap, <span style="color: #0000ff;">int</span> i); <span style="color: #008000;">//</span><span style="color: #008000;"> 从下标为i的位置开始打印其父节点的值,直到到达数根为止</span>
<span style="color: #0000ff;">int</span> main(<span style="color: #0000ff;">void</span><span style="color: #000000;">)
{
</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n, m, data, i;
PHEAP pHeap;

scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d%d</span><span style="color: #800000;">"</span>, &amp;n, &amp;<span style="color: #000000;">m);
pHeap </span>=<span style="color: #000000;"> CreateHeap(CAPACITY);
</span><span style="color: #0000ff;">while</span>(n--<span style="color: #000000;">)
{
scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, &amp;<span style="color: #000000;">data);
Insert(pHeap, data);
}
</span><span style="color: #0000ff;">while</span>(m--<span style="color: #000000;">)
{
scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, &amp;<span style="color: #000000;">i);
PrintPath(pHeap, i);
printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);
}

</span><span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">;
}

PHEAP CreateHeap(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> capacity)
{
PHEAP pHeap </span>= (PHEAP)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(HEAP));
pHeap</span>-&gt;arr = (<span style="color: #0000ff;">int</span> *)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span>(<span style="color: #0000ff;">int</span>) * (capacity+<span style="color: #800080;">1</span><span style="color: #000000;">));
pHeap</span>-&gt;arr[<span style="color: #800080;">0</span>] = MIN; <span style="color: #008000;">//</span><span style="color: #008000;"> arr[0]做哨兵,方便查找操作</span>
pHeap-&gt;size = <span style="color: #800080;">0</span><span style="color: #000000;">;
pHeap</span>-&gt;capacity =<span style="color: #000000;">capacity;

</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> pHeap;
}

</span><span style="color: #0000ff;">void</span> Insert(PHEAP pHeap, <span style="color: #0000ff;">int</span><span style="color: #000000;"> data)
{
</span><span style="color: #0000ff;">int</span> position = ++pHeap-&gt;size; <span style="color: #008000;">//</span><span style="color: #008000;"> 应该先判堆是否满的,因为测试数据不会导致堆满,因此判断堆是否满的操作省略了</span>

<span style="color: #0000ff;">for</span>(; data &lt; pHeap-&gt;arr[position/<span style="color: #800080;">2</span>]; position /= <span style="color: #800080;">2</span><span style="color: #000000;">)
pHeap</span>-&gt;arr[position] = pHeap-&gt;arr[position/<span style="color: #800080;">2</span>]; <span style="color: #008000;">//</span><span style="color: #008000;"> 向下(数根)过滤节点</span>
<span style="color: #000000;">
pHeap</span>-&gt;arr[position] =<span style="color: #000000;"> data;
}

</span><span style="color: #0000ff;">void</span> PrintPath(PHEAP pHeap, <span style="color: #0000ff;">int</span> i) <span style="color: #008000;">//</span><span style="color: #008000;"> 从下标为i的位置开始打印其父节点的值,直到到达数根为止</span>
<span style="color: #000000;">{
</span><span style="color: #0000ff;">int</span> isFirst = <span style="color: #800080;">1</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(i)
{
</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(isFirst)
isFirst </span>= <span style="color: #800080;">0</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">else</span><span style="color: #000000;">
printf(</span><span style="color: #800000;">"</span> <span style="color: #800000;">"</span><span style="color: #000000;">);
printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, pHeap-&gt;<span style="color: #000000;">arr[i]);
i </span>/= <span style="color: #800080;">2</span><span style="color: #000000;">;
}
}</span></span></pre>
<pre class="code"><span style="font-family: %value; font-size: 14px;">05-树7. File Transfer (25)
We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it possible to send a file from any computer on the network to any other?

Input Specification:

Each input file contains one test case. For each test case, the first line contains N (2&lt;=N&lt;=104), the total number of computers in a network. Each computer in the network is then represented by a positive integer between 1 and N. Then in the following lines, the input is given in the format:

I c1 c2
where I stands for inputting a connection between c1 and c2; or

C c1 c2
where C stands for checking if it is possible to transfer files between c1 and c2; or

S
where S stands for stopping this case.

Output Specification:

For each C case, print in one line the word "yes" or "no" if it is possible or impossible to transfer files between c1 and c2, respectively. At the end of each case, print in one line "The network is connected." if there is a path between any pair of computers; or "There are k components." where k is the number of connected components in this network.

Sample Input 1:
5
C 3 2
I 3 2
C 1 5
I 4 5
I 2 4
C 3 5
S
Sample Output 1:
no
no
yes
There are 2 components.
Sample Input 2:
5
C 3 2
I 3 2
C 1 5
I 4 5
I 2 4
C 3 5
I 1 3
C 1 5
S
Sample Output 2:
no
no
yes
yes
The network is connected.
</span></pre>
<pre class="code"><span style="font-family: %value; font-size: 14px;"><span style="color: #008000;">/*</span><span style="color: #008000;">
写的这个代码有点自注释的感觉了,因此不写思路,它们都藏在我的代码里:~
</span><span style="color: #008000;">*/</span><span style="color: #000000;">
#include</span>&lt;stdio.h&gt; <span style="color: #008000;">//</span><span style="color: #008000;"> 经测试,路径压缩和高度差合并两个任取一个实现之就能AC,但是毫无疑问,两者皆实现能够保证最快的速度因而更保险</span>
#include&lt;<span style="color: #0000ff;">string</span>.h&gt;
<span style="color: #0000ff;">#define</span> MAXN 10000<span style="color: #000000;">
typedef </span><span style="color: #0000ff;">struct</span> <span style="color: #0000ff;">set</span><span style="color: #000000;">
{
</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> data;
</span><span style="color: #0000ff;">int</span> parent; <span style="color: #008000;">//</span><span style="color: #008000;"> 采用数组下标的方式指示它的父节点</span>
<span style="color: #000000;">}SET;

SET </span><span style="color: #0000ff;">set</span>[MAXN+<span style="color: #800080;">10</span>]; <span style="color: #008000;">//</span><span style="color: #008000;"> 方便起见,0号单元空着了,每个集合的根节点的parent置为一个负数</span>

<span style="color: #0000ff;">int</span> Find(SET <span style="color: #0000ff;">set</span>[], <span style="color: #0000ff;">int</span> element); <span style="color: #008000;">//</span><span style="color: #008000;"> 查找元素属于哪个集合,返回元素所在集合的根,未找到则返回-1</span>
<span style="color: #0000ff;">void</span> Union(SET <span style="color: #0000ff;">set</span>[], <span style="color: #0000ff;">int</span> element1, <span style="color: #0000ff;">int</span> element2); <span style="color: #008000;">//</span><span style="color: #008000;"> 将两个元素合并到一个集合中,方便起见,元素值放到对应下标中</span>
<span style="color: #0000ff;">int</span> SetCount(SET <span style="color: #0000ff;">set</span>[], <span style="color: #0000ff;">int</span> n); <span style="color: #008000;">//</span><span style="color: #008000;"> 统计n个元素构成的集合总数</span>

<span style="color: #0000ff;">int</span> main(<span style="color: #0000ff;">void</span><span style="color: #000000;">)
{
</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n, element1, element2, setCount;
</span><span style="color: #0000ff;">char</span><span style="color: #000000;"> ch;

memset(</span><span style="color: #0000ff;">set</span>, <span style="color: #800080;">0</span>, <span style="color: #0000ff;">sizeof</span>(<span style="color: #0000ff;">set</span><span style="color: #000000;">));
scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, &amp;<span style="color: #000000;">n);
</span><span style="color: #0000ff;">while</span>(scanf(<span style="color: #800000;">"</span><span style="color: #800000;"> %c</span><span style="color: #800000;">"</span>, &amp;ch) &amp;&amp; ch != <span style="color: #800000;">‘</span><span style="color: #800000;">S</span><span style="color: #800000;">‘</span><span style="color: #000000;">)
{
scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d%d</span><span style="color: #800000;">"</span>, &amp;element1, &amp;<span style="color: #000000;">element2);
</span><span style="color: #0000ff;">if</span>(ch == <span style="color: #800000;">‘</span><span style="color: #800000;">C</span><span style="color: #800000;">‘</span><span style="color: #000000;">)
{
</span><span style="color: #0000ff;">if</span>(Find(<span style="color: #0000ff;">set</span>, element1) != -<span style="color: #800080;">1</span> &amp;&amp; Find(<span style="color: #0000ff;">set</span>, element1) == Find(<span style="color: #0000ff;">set</span><span style="color: #000000;">, element2))
printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">yes\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">else</span><span style="color: #000000;">
printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">no\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);
}
</span><span style="color: #0000ff;">else</span><span style="color: #000000;">
Union(</span><span style="color: #0000ff;">set</span><span style="color: #000000;">, element1, element2);
}

setCount </span>= SetCount(<span style="color: #0000ff;">set</span><span style="color: #000000;">, n);
</span><span style="color: #0000ff;">if</span>(setCount == <span style="color: #800080;">1</span><span style="color: #000000;">)
printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">The network is connected.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">else</span><span style="color: #000000;">
printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">There are %d components.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">, setCount);

</span><span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">;
}

</span><span style="color: #0000ff;">int</span> Find(SET <span style="color: #0000ff;">set</span>[], <span style="color: #0000ff;">int</span><span style="color: #000000;"> element)
{
</span><span style="color: #0000ff;">int</span> root =<span style="color: #000000;"> element;
</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> higher;

</span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">set</span>[element].data != element) <span style="color: #008000;">//</span><span style="color: #008000;"> 因为set数组初始为0,而给定的最小输入是2,因此元素不在集合中的话,这个条件成立</span>
<span style="color: #0000ff;">return</span> -<span style="color: #800080;">1</span><span style="color: #000000;">;

</span><span style="color: #008000;">//</span><span style="color: #008000;"> for(; set[element].parent &gt; 0; element = set[element].parent) </span><span style="color: #008000;">//</span><span style="color: #008000;"> 没有路径压缩,直接超时
</span><span style="color: #008000;">//</span><span style="color: #008000;"> ;
</span><span style="color: #008000;">//</span><span style="color: #008000;"> return element;</span>

<span style="color: #0000ff;">for</span>(; <span style="color: #0000ff;">set</span>[root].parent &gt; <span style="color: #800080;">0</span>; root = <span style="color: #0000ff;">set</span><span style="color: #000000;">[root].parent)
;
</span><span style="color: #0000ff;">while</span>(<span style="color: #0000ff;">set</span>[element].parent &gt; <span style="color: #800080;">0</span><span style="color: #000000;">)
{
higher </span>= <span style="color: #0000ff;">set</span><span style="color: #000000;">[element].parent;
</span><span style="color: #0000ff;">set</span>[element].parent =<span style="color: #000000;"> root;
element </span>=<span style="color: #000000;"> higher;
}

</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> root;
}

</span><span style="color: #0000ff;">void</span> Union(SET <span style="color: #0000ff;">set</span>[], <span style="color: #0000ff;">int</span> element1, <span style="color: #0000ff;">int</span> element2) <span style="color: #008000;">//</span><span style="color: #008000;"> 高度小的树并到高度大的树上,|parent|代表树的高度(以集合元素个数作为度量标准)</span>
<span style="color: #000000;">{
</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> root1, root2;

</span><span style="color: #0000ff;">if</span>(Find(<span style="color: #0000ff;">set</span>, element1) == -<span style="color: #800080;">1</span>) <span style="color: #008000;">//</span><span style="color: #008000;"> 如果元素不在集合中,则将其单独构成一个只包含其本身的集合</span>
<span style="color: #000000;"> {
</span><span style="color: #0000ff;">set</span>[element1].data =<span style="color: #000000;"> element1;
</span><span style="color: #0000ff;">set</span>[element1].parent = -<span style="color: #800080;">1</span><span style="color: #000000;">;
}
</span><span style="color: #0000ff;">if</span>(Find(<span style="color: #0000ff;">set</span>, element2) == -<span style="color: #800080;">1</span><span style="color: #000000;">)
{
</span><span style="color: #0000ff;">set</span>[element2].data =<span style="color: #000000;"> element2;
</span><span style="color: #0000ff;">set</span>[element2].parent = -<span style="color: #800080;">1</span><span style="color: #000000;">;
}

root1 </span>= Find(<span style="color: #0000ff;">set</span><span style="color: #000000;">, element1);
root2 </span>= Find(<span style="color: #0000ff;">set</span><span style="color: #000000;">, element2);
</span><span style="color: #0000ff;">if</span>(root1 !=<span style="color: #000000;"> root2)
{

</span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">set</span>[root1].parent &lt; <span style="color: #0000ff;">set</span>[root2].parent) <span style="color: #008000;">//</span><span style="color: #008000;"> 如果root1的高度高,就把root2挂到root1上,同时修改root1的高度</span>
<span style="color: #000000;"> {
</span><span style="color: #0000ff;">set</span>[root1].parent += <span style="color: #0000ff;">set</span><span style="color: #000000;">[root2].parent;
</span><span style="color: #0000ff;">set</span>[root2].parent =<span style="color: #000000;"> root1;
}
</span><span style="color: #0000ff;">else</span><span style="color: #000000;">
{
</span><span style="color: #0000ff;">set</span>[root2].parent += <span style="color: #0000ff;">set</span><span style="color: #000000;">[root1].parent;
</span><span style="color: #0000ff;">set</span>[root1].parent =<span style="color: #000000;"> root2;
}

</span><span style="color: #008000;">//</span><span style="color: #008000;"> set[root2].parent = root1; </span><span style="color: #008000;">//</span><span style="color: #008000;">没有高度控制的代码,在if(root1 != root2)里仅此一行</span>
<span style="color: #000000;">
}

</span><span style="color: #0000ff;">return</span><span style="color: #000000;">;
}

</span><span style="color: #0000ff;">int</span> SetCount(SET <span style="color: #0000ff;">set</span>[], <span style="color: #0000ff;">int</span><span style="color: #000000;"> n)
{
</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i, count;

count </span>= <span style="color: #800080;">0</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">for</span>(i = <span style="color: #800080;">1</span>; i &lt;= n; i++<span style="color: #000000;">)
{
</span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">set</span>[i].parent &lt;= <span style="color: #800080;">0</span>) <span style="color: #008000;">//</span><span style="color: #008000;"> 小于零的元素代表添加到集合中的一个根,等于零的元素代表未添加到集合中的元素,它独自构成一个集合</span>
count++<span style="color: #000000;">;
}

</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> count;
}</span></span></pre>
<p>&nbsp;</p>
<p>(XP_JIANG).</p>

时间: 2024-12-26 11:24:52

PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由的相关文章

二叉树遍历的应用(路径和问题,判断是否是二叉搜索树,判断是否是二叉平衡树)

现在越发觉得关于树的问题真是千变万化,随便改一个条件又会是一个新的问题. 先序遍历求二叉树路径问题 问题:一棵二叉树每个节点包含一个整数,请设计一个算法输出所有满足条件的路径:此路径上所有节点之和等于给定值.注意此类路径不要求必须从根节点开始. 如果没有最后一个条件,这道题在leetcode上面见过,就是采取先序遍历的方式并记录下路径.但是加上最后一个条件后需要转下弯思考一下. 当然也需要记录下路径,并且记得要回溯.加上不要求从根节点开始这个条件后可以这么思考,每次遍历到当前节点时,从路径中查找

PTA L2-004 这是二叉搜索树吗?-判断是否是对一棵二叉搜索树或其镜像进行前序遍历的结果 团体程序设计天梯赛-练习集

L2-004 这是二叉搜索树吗? (25 分) 一棵二叉搜索树可被递归地定义为具有下列性质的二叉树:对于任一结点, 其左子树中所有结点的键值小于该结点的键值: 其右子树中所有结点的键值大于等于该结点的键值: 其左右子树都是二叉搜索树. 所谓二叉搜索树的“镜像”,即将所有结点的左右子树对换位置后所得到的树. 给定一个整数键值序列,现请你编写程序,判断这是否是对一棵二叉搜索树或其镜像进行前序遍历的结果. 输入格式: 输入的第一行给出正整数 N(≤).随后一行给出 N 个整数键值,其间以空格分隔. 输

L2-004 这是二叉搜索树吗? (25 分) (树)

链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805070971912192 题目: 一棵二叉搜索树可被递归地定义为具有下列性质的二叉树:对于任一结点, 其左子树中所有结点的键值小于该结点的键值: 其右子树中所有结点的键值大于等于该结点的键值: 其左右子树都是二叉搜索树. 所谓二叉搜索树的“镜像”,即将所有结点的左右子树对换位置后所得到的树. 给定一个整数键值序列,现请你编写程序,判断这是否是对一棵二叉搜索树或其

剑指Offer对答如流系列 - 二叉搜索树的后序遍历序列

面试题33:二叉搜索树的后序遍历序列 题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true,否则返回false.假设输入的数组的任意两个数字都互不相同. 例如,输入数组{5.7.6.9.11.10.8},则返回true,因为这个整数序列是下图二叉搜索树的后序遍历结果. 如果输入的数组是{7.4.6.5},则由于没有哪棵二叉搜索树的后序遍历是这个序列,因此返回false. 问题分析 后序遍历 遍历顺序是 :左子树 -> 右子树 -> 根结点(最后遍历根节

数据结构——二叉搜索树、B树、B-树

数据结构——二叉搜索树.B树.B-树 1. 综述 二叉排序树(Binary Sort Tree),又叫二叉查找树(Binary Search Tree),也叫二叉排序树. 二叉搜索树满足以下性质: 1. 若根节点左子树不为空,则左子树上的所有节点均小于根节点: 2. 若根节点右子树不为空,则右子树上的所有节点均大于根节点: 3. 其左右子树也是二叉搜索树(递归定义): 4. 没有键值相等的点. B树就是B-树.B树/B-树英文叫B-Tree,可能被不小心翻译成了B-树.

Java实现二叉搜索树的添加,前序、后序、中序及层序遍历,求树的节点数,求树的最大值、最小值,查找等操作

什么也不说了,直接上代码. 首先是节点类,大家都懂得 /** * 二叉树的节点类 * * @author HeYufan * * @param <T> */ class Node<T extends Comparable<? super T>> { /** * 节点储存的值 */ private T data; /** * 左子节点 */ private Node<T> leftNode; /** * 右子节点 */ private Node<T>

树(二叉树)的建立和遍历算法(一)(前序,中序,后序)

最近学习树的概念,有关二叉树的实现算法记录下来... 不过学习之前要了解的预备知识:树的概念:二叉树的存储结构:二叉树的遍历方法.. 二叉树的存储结构主要了解二叉链表结构,也就是一个数据域,两个指针域,(分别为指向左右孩子的指针),从下面程序1,二叉树的存储结构可以看出. 二叉树的遍历方法:主要有前序遍历,中序遍历,后序遍历,层序遍历.(层序遍历下一篇再讲,本篇主要讲的递归法) 如这样一个二叉树: 它的前序遍历顺序为:ABDGHCEIF(规则是先是根结点,再前序遍历左子树,再前序遍历右子树) 它

二叉树各种相关操作(建立二叉树、前序、中序、后序、求二叉树的深度、查找二叉树节点,层次遍历二叉树等)(C语言版)

将二叉树相关的操作集中在一个实例里,有助于理解有关二叉树的相关操作: 1.定义树的结构体: 1 typedef struct TreeNode{ 2 int data; 3 struct TreeNode *left; 4 struct TreeNode *right; 5 }TreeNode; 2.创建根节点: 1 TreeNode *creatRoot(){ 2 TreeNode * root =(TreeNode *)malloc(sizeof(TreeNode)); 3 if(NULL=

C语言非递归实现二叉树的先序、中序、后序、层序遍历

C语言非递归实现二叉树的先序.中序.后序.层序遍历代码如下: 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <stack> 4 #include <queue> 5 using namespace std; 6 7 //*****二叉树的二叉链表存储表示*****// 8 typedef struct BiNode 9 { 10 char data; 11 struct BiNode *lchil