zoukankan      html  css  js  c++  java
  • JAVA按层级遍历二叉树

    /**
     * @author cj 2017年7月14日下午1:14:31
     */
    package com.yuanye.algorithm;
    
    import java.util.LinkedList;
    import java.util.List;
    
    public class BinaryTree {
    //    private static List<Node> currentLevelNodes=new ArrayList<>();
    //    private static List<Node> nextLevelNodes=new ArrayList<>();
        private static List<Node> nodeList=new LinkedList<>();
        
        public static void main(String[] args) {
            Node rootNode=new Node();
            rootNode.setValue(88);
            generateTree(rootNode,5,0);
            printTree(rootNode);
        }
        public static void generateTree(Node parentNode,int depth,int currentDepth){
            if(currentDepth>depth-1)
                return;
            Node leftNode=new Node();
            leftNode.setValue(6000+currentDepth);
            Node rightNode=new Node();
            rightNode.setValue(9000+currentDepth);
            parentNode.setLeftNode(leftNode);
            parentNode.setRightNode(rightNode);
            generateTree(leftNode,depth,currentDepth+1);
            generateTree(rightNode,depth,currentDepth+1);
        }
        public static void printTree(Node rootNode){
            System.out.println(rootNode.getValue());
            Node leftNode=rootNode.getLeftNode();
            Node rightNode=rootNode.getRightNode();
            if(leftNode!=null){
                nodeList.add(leftNode);
            }
            if(rightNode!=null){
                nodeList.add(rightNode);
            }
    //        if(currentLevelNodes.size()==0){
    //            currentLevelNodes.addAll(nextLevelNodes);
    //            nextLevelNodes.removeAll(nextLevelNodes);
    //        }
    //        if(currentLevelNodes.size()>0){
    //            Node nextNode=currentLevelNodes.get(0);
    //            currentLevelNodes.remove(0);
    //            printTree(nextNode);
    //        }
            if(nodeList.size()>0){
                Node nextNode=nodeList.get(0);
                nodeList.remove(0);
                printTree(nextNode);
            }
        }
    }
    class Node{
        private int value;
        private Node leftNode;
        private Node rightNode;
        
        public int getValue() {
            return value;
        }
        public void setValue(int value) {
            this.value = value;
        }
        public Node getLeftNode() {
            return leftNode;
        }
        public void setLeftNode(Node leftNode) {
            this.leftNode = leftNode;
        }
        public Node getRightNode() {
            return rightNode;
        }
        public void setRightNode(Node rightNode) {
            this.rightNode = rightNode;
        }
        @Override
        public String toString() {
            return "Node [value=" + value + ", leftNode=" + leftNode + ", rightNode=" + rightNode + "]";
        }
    }
  • 相关阅读:
    HDU 5392 Infoplane in Tina Town
    HDU 2206 IP的计算(字符串处理)
    线程的条件变量实例
    CentOS安装配置Samba
    PO订单审批拒绝API
    【Java集合源代码剖析】Hashtable源代码剖析
    magento megatron主题加入中文
    递归系列2(字符串翻转,12345翻转)
    机器学习之&amp;&amp;Andrew Ng课程复习--- 聚类——Clustering
    JSP基础
  • 原文地址:https://www.cnblogs.com/yuanye007/p/7189742.html
Copyright © 2011-2022 走看看