zoukankan      html  css  js  c++  java
  • Java实现二叉树的四种遍历

      1 package Tree;
      2 
      3 import java.util.ArrayDeque;
      4 import java.util.Queue;
      5 import java.util.Stack;
      6 
      7 /**
      8  * Created by lenovo on 2017/9/6.
      9  */
     10 public class BinaryTree {
     11     /*
     12     * 前序遍历,递归实现
     13     * */
     14     public void PreOrder(TreeNode node) {
     15         if (node != null) {
     16             System.out.print(node.val);
     17             PreOrder(node.left);
     18             PreOrder(node.right);
     19         }
     20     }
     21 
     22     /*
     23     * 前序遍历,非递归实现
     24     * 1,先入栈根节点,输出根节点val值,再先后入栈其右节点、左结点;
     25     * 2,出栈左节点,输出其val值,再入栈该左节点的右节点、左节点;直到遍历完该左节点所在子树。
     26     * 3,再出栈右节点,输出其val值,再入栈该右节点的右节点、左节点;直到遍历完该右节点所在子树。
     27     * */
     28     public void PreOrder1(TreeNode root) {
     29         Stack<TreeNode> stack = new Stack<>();
     30         if (root != null) {
     31             stack.push(root);
     32         }
     33         while (!stack.empty()) {
     34             TreeNode node = stack.pop();
     35             System.out.print(node.val);
     36             //右结点先入栈,左结点后入栈
     37             if (node.right != null) stack.push(node.right);
     38             if (node.left != null) stack.push(node.left);
     39         }
     40     }
     41 
     42     /*
     43     * 中序遍历,递归实现
     44     * */
     45     public void InOrder(TreeNode node) {
     46         if (node != null) {
     47             InOrder(node.left);
     48             System.out.print(node.val);
     49             InOrder(node.right);
     50         }
     51     }
     52 
     53     /*
     54    * 中序遍历,非递归实现
     55    * 1,首先从根节点出发一路向左,入栈所有的左节点;
     56    * 2,出栈一个节点,输出该节点val值,查询该节点是否存在右节点,
     57    * 若存在则从该右节点出发一路向左入栈该右节点所在子树所有的左节点;
     58    * 3,若不存在右节点,则出栈下一个节点,输出节点val值,同步骤2操作;
     59    * 4,直到节点为null,且栈为空。
     60    * */
     61     public void InOrder1(TreeNode root) {
     62         Stack<TreeNode> stack = new Stack<>();
     63         while (root != null || !stack.empty()) {
     64             while (root != null) {
     65                 stack.push(root);
     66                 root = root.left;
     67             }
     68             if (!stack.empty()) {
     69                 TreeNode node = stack.pop();
     70                 System.out.print(node.val);
     71                 root = node.right;
     72             }
     73         }
     74     }
     75 
     76     /*
     77     * 后序遍历,递归实现
     78     * */
     79     public void PostOrder(TreeNode node) {
     80         if (node != null) {
     81             PostOrder(node.left);
     82             PostOrder(node.right);
     83             System.out.print(node.val);
     84         }
     85     }
     86 
     87     /*
     88     * 层序遍历(广度优先遍历)
     89     * */
     90     public void LayerOrder(TreeNode root) {
     91         Queue<TreeNode> queue = new ArrayDeque<>();
     92         if (root != null) queue.offer(root);
     93         while (!queue.isEmpty()) {
     94             TreeNode node = queue.poll();
     95             System.out.print(node.val);
     96             if (node.left != null) queue.offer(node.left);
     97             if (node.right != null) queue.offer(node.right);
     98         }
     99     }
    100 }
  • 相关阅读:
    conda安装opencv opencv-contrib-python opencv-python
    在Conda下安装jupyter notebook
    安装eric环境小记
    https://paperswithcode.com/task/object-detection
    全网最全开源工业缺陷数据集汇总(已更新24个)
    caffe windows训练测试自己的图片
    caffe训练自己的图片(分类)
    caffe学习系列:训练自己的图片集(超详细教程)
    conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch
    回旋曲线的计算
  • 原文地址:https://www.cnblogs.com/gxclmx/p/7485384.html
Copyright © 2011-2022 走看看