zoukankan      html  css  js  c++  java
  • 二叉树的三种遍历方式

    一、二叉树的定义

    二叉树(Binary Tree)的递归定义:二叉树要么为空,要么由根节点(root)、左子树(left subtree)和右子树(right subtree)组成,而左子书和右子树分别是一颗二叉树。注意,在计算机中,树一般是"倒置"的,即根在上,叶子在下。

    二、二叉树的层次遍历

    三种遍历方式:先序遍历、中序遍历、后序遍历(根据根节点的顺序)

    PreOrder(T) = T的根节点 + PreOrder(T的左子树) + PreOrder(T的右子树)

    InOrder(T) = InOrder(T的左子树) + T的根节点 + InOrder(T的右子树)

    PostOrder(T) = PostOrder(左子树) + PostOrder(右子树)

    其中加号表示字符串连接

    这三种遍历都是递归遍历或者说深度优先遍历 (DFS,Depth-First-Search)

    三、已知两种遍历方式,推出另一种遍历方式

    先序+中序---->后序

    后序+中序---->先序

    因为后序或先序可以直接得到根节点,然后只要在中序遍历中找到,就知道左右子树的中序和后序遍历,递归下去就可以构造出二叉树了。

    四、样例

    (1) 题意:给一颗点带权(各权值都不相同,都是小于10000的整数)的二叉树的中序和后序遍历,找一个叶子节点使它到根的路径上的权应尽量少。

    (2) 代码实现:

     1 #include<stdio.h>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<string>
     6 #include<sstream>
     7 using namespace std;
     8 
     9 const int INF = 0x3f3f3f3f;
    10 //因为各节点的权值各不相同且都只是整数,直接用权值作为节点编号
    11 const int maxn = 10000 + 10;    
    12 int in_order[maxn], post_order[maxn], lch[maxn], rch[maxn];
    13 int n;
    14 int best, best_sum;
    15 
    16 //按行读取数据,并存到数组中
    17 bool read_list(int *a)
    18 {
    19     string line;
    20     if (!getline(cin, line))  return false;
    21     stringstream ss(line);
    22     n = 0;
    23     int x;
    24     while (ss >> x)  a[n++] = x;
    25     return n > 0;
    26 }
    27 
    28 //把in_order[L1,R1]和post_order[L2,R2]建成一棵二叉树,返回树根
    29 int build(int L1, int R1, int L2, int R2)
    30 {
    31     if (L2 > R2)  return 0;  //空树
    32     int root = post_order[R2];
    33     int pos = L1;
    34     while (in_order[pos] != root)  pos++;
    35     int cnt = pos - L1;
    36     lch[root] = build(L1, pos - 1, L2, L2 + cnt - 1);
    37     rch[root] = build(pos + 1, R1, L2 + cnt, R2 - 1);
    38     return root;
    39 }
    40 
    41 //从根节点出发,中序遍历,查找最小值
    42 void dfs(int u, int sum)
    43 {
    44     sum += u;
    45 
    46     //到达叶子节点,循环终止
    47     if (!lch[u] && !rch[u])
    48     {
    49         if (sum < best_sum)
    50         {
    51             best = u;
    52             best_sum = sum;
    53         }
    54         return;
    55     }
    56 
    57     //加了个剪枝:如果当前的和大于当前的最小和,就不必从这条路继续搜
    58     if (lch[u] && sum < best_sum)  dfs(lch[u], sum);
    59     if (rch[u] && sum < best_sum)  dfs(rch[u], sum);
    60 }
    61 
    62 int main()
    63 {
    64     while (read_list(in_order))
    65     {
    66         read_list(post_order);
    67         build(0, n - 1, 0, n - 1);
    68 
    69         best_sum = INF;
    70         dfs(post_order[n - 1], 0);
    71         cout << best << endl;
    72     }
    73     return 0;
    74 }
  • 相关阅读:
    SGU 176.Flow construction (有上下界的最大流)
    POJ 2391.Ombrophobic Bovines (最大流)
    poj 1087.A Plug for UNIX (最大流)
    poj 1273.PIG (最大流)
    POJ 2112.Optimal Milking (最大流)
    SGU 196.Matrix Multiplication
    SGU 195. New Year Bonus Grant
    关于multicycle path
    ppt做gif动图
    codeforces 598A Tricky Sum
  • 原文地址:https://www.cnblogs.com/lfri/p/9504865.html
Copyright © 2011-2022 走看看