zoukankan      html  css  js  c++  java
  • 二叉树的层次遍历

    思路:

    (1) 非递归思路: 队列

    (2) 递归思路

    package com.zhaochao.tree;

    import java.lang.reflect.Array;
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Queue;

    /**
    * Created by zhaochao on 17/1/23.
    */
    public class LeverTraversal {


    public ArrayList<List<Integer>> leverOrder(TreeNode root) {

    ArrayList<List<Integer>> result = new ArrayList<List<Integer>>();

    if(root == null) {
    return result;
    }

    Queue<TreeNode> queue = new LinkedList<TreeNode>();
    queue.add(root);

    while(!queue.isEmpty()) {
    List<Integer> lever = new ArrayList<Integer>();
    int size = queue.size();
    for(int i = 0; i < size; i++) {
    TreeNode head = queue.poll();
    lever.add(head.val);
    if(head.left != null) {
    queue.add(head.left);
    }
    if(head.right != null) {
    queue.add(head.right);
    }
    }
    result.add(lever);
    }

    return result;
    }

    public static void main(String[] args) {

    TreeNode root = new TreeNode(0);
    TreeNode node1 = new TreeNode(1);
    TreeNode node2 = new TreeNode(2);
    TreeNode node3 = new TreeNode(3);

    root.left = node1;
    root.right = node2;
    node2.left = node3;

    LeverTraversal test = new LeverTraversal();
    ArrayList<List<Integer>> result = new ArrayList<List<Integer>>();
    result = test.leverOrder(root);
    for(int i = 0; i < result.size(); i++) {
    List<Integer> tmp = new ArrayList<Integer>();
    tmp = result.get(i);
    for(int j = 0; j < tmp.size(); j++) {
    System.out.print(tmp.get(j) + " ");
    }
    System.out.println();
    }
    }
    }



    递归思路:

    package com.zhaochao.tree;

    import java.lang.reflect.Array;
    import java.util.ArrayList;
    import java.util.List;

    /**
    * Created by zhaochao on 17/1/23.
    */
    public class LevelOrderRecursion {


    public ArrayList<ArrayList<Integer>> leverOrder(TreeNode root) {

    ArrayList<ArrayList<Integer>> result =new ArrayList<ArrayList<Integer>>();
    if(root == null) {
    return result;
    }
    helper(result, 0, root);
    return result;
    }


    public void helper(ArrayList<ArrayList<Integer>> result, int level, TreeNode root) {
    if(root == null) {
    return;
    }

    ArrayList<Integer> curr;
    if(level >= result.size()) {
    curr = new ArrayList<Integer>();
    curr.add(root.val);
    result.add(curr);
    } else {
    curr = result.get(level);
    curr.add(root.val);
    }

    helper(result, level + 1, root.left);
    helper(result, level + 1, root.right);
    }


    public static void main(String[] args) {

    TreeNode root = new TreeNode(0);
    TreeNode node1 = new TreeNode(1);
    TreeNode node2 = new TreeNode(2);
    TreeNode node3 = new TreeNode(3);

    root.left = node1;
    root.right = node2;
    node2.left = node3;

    LevelOrderRecursion test = new LevelOrderRecursion();

    ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
    result = test.leverOrder(root);
    for(int i = 0; i < result.size(); i++) {
    List<Integer> tmp = new ArrayList<Integer>();
    tmp = result.get(i);
    for(int j = 0; j < tmp.size(); j++) {
    System.out.print(tmp.get(j) + " ");
    }
    System.out.println();
    }

    }
    }
  • 相关阅读:
    Spark
    升级测试数据迁移数据库版本不兼容的问题:mysql5.7 timestamp默认值0000-00-00 00:00:00 报错
    Redis
    批处理引擎MapReduce
    分布式协调服务ZooKeeper
    分布式列式存储系统Kudu
    Python入门学习笔记9:Python高级语法与用法-枚举、函数式编程<闭包>
    Python入门学习笔记8:正则表达式与JSON
    Python入门学习笔记7:面向对象
    Python入门学习笔记6:函数
  • 原文地址:https://www.cnblogs.com/superzhaochao/p/6345171.html
Copyright © 2011-2022 走看看