JAVA按層級遍曆二叉樹

來源:互聯網
上載者:User

標籤:sys   tree   eal   void   log   size   his   link   com   

/** * @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 + "]";    }}

 

JAVA按層級遍曆二叉樹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.