• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
kohakuhubo
博客园    首页    新随笔    联系   管理    订阅  订阅

二叉树的遍历算法

二叉树的遍历算法
 1 package com.berry.algorithm.tree;
 2 
 3 /**
 4  * Created by berry-h on 17-1-16.
 5  * 树的节点类(结构体)
 6  */
 7 public class TreeNode {
 8 
 9     public int value;
10 
11     public TreeNode leftChild;
12 
13     public TreeNode rightChild;
14 
15 }
 1 package com.berry.algorithm.tree;
 2 
 3 /**
 4  * Created by berry-h on 17-1-16.
 5  */
 6 public class TreeTraversalAlgorithm {
 7 
 8     /**
 9      *
10      * 树的前序遍历算法
11      */
12     public static void dlr(TreeNode treeNode){
13 
14         if(treeNode == null){
15             return;
16         }
17 
18         System.out.println("前序算法 value: " + treeNode.value);
19         dlr(treeNode.leftChild);
20         dlr(treeNode.rightChild);
21     }
22 
23 
24     /**
25      *
26      * 树的中序遍历算法
27      */
28     public static void ldr(TreeNode treeNode){
29 
30         if(treeNode == null){
31             return;
32         }
33 
34         ldr(treeNode.leftChild);
35         System.out.println("中序算法 value: " + treeNode.value);
36         ldr(treeNode.rightChild);
37     }
38 
39     /**
40      *
41      * 树的后序遍历算法
42      */
43     public static void lrd(TreeNode treeNode){
44 
45         if(treeNode == null){
46             return;
47         }
48 
49         lrd(treeNode.leftChild);
50         lrd(treeNode.rightChild);
51         System.out.println("后序算法 value: " + treeNode.value);
52     }
53 }

 

posted @ 2017-01-17 16:32  kohakuhubo  阅读(74)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3