按照箭头方向查找二叉树

2019-12-19 05:49:05来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

按照箭头方向查找二叉树

花了半个小时,具体实现代码如下

package com.trs.codetool.core;


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author zheng.changgang
 * @date 2019-12-18 09:44
 */
public class TreeNodeTest {

    public static void main(String[] args) {
        Node head = new Node(1);
        head.left = new Node(2);
        head.right = new Node(3);

        head.left.left = new Node(4);
        head.left.right = new Node(5);

        head.right.right = new Node(6);

        head.left.left.left = new Node(7);
        head.left.left.right = new Node(8);
        // 每一行打印出来
        printNode(head);

    }

    private static void printNode(Node head) {
        Queue<Node> queue = new LinkedList<>();
        List<List<Integer>> resultList = new ArrayList();
        List<Integer> rowList = new ArrayList();
        queue.offer(head);
        Node last = head;
        Node nlast = head;
        while (!queue.isEmpty()) {
            Node cur = queue.poll();
            if(cur.left != null) {
                queue.offer(cur.left);
                nlast  = cur.left;
            }
            if(cur.right != null) {
                queue.offer(cur.right);
                nlast  = cur.right;
            }

            if(cur == last) {
               // System.out.println(cur.getValue());
                last = nlast;
                rowList.add(cur.getValue());
                resultList.add(rowList);
                rowList = new ArrayList();
            }else {
                rowList.add(cur.getValue());
                //System.out.print(cur.getValue() + " ");
            }
        }
        for(int i=0;i<resultList.size();i++) {
            List<Integer> list = resultList.get(i);
            if(i % 2 == 0) {
                for(int j=0;j<list.size();j++) {
                    System.out.print(list.get(j)+" ");
                }
            } else {
                for(int j=list.size()-1;j>=0;j--) {
                    System.out.print(list.get(j)+" ");
                }
            }
            System.out.println();
        }
    }

    static class Node {
        int value;
        Node left;
        Node right;

        public Node(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public Node getLeft() {
            return left;
        }

        public void setLeft(Node left) {
            this.left = left;
        }

        public Node getRight() {
            return right;
        }

        public void setRight(Node right) {
            this.right = right;
        }
    }
}

原文链接:https://www.cnblogs.com/zcg1051980588/p/12066114.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:我采访了同事,让他掏出了每天都会浏览的干货网站...

下一篇:线程提供的方法:static void sleep(long ms),会进入阻塞状态,