zoukankan      html  css  js  c++  java
  • 按照箭头方向查找二叉树

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

    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;
            }
        }
    }
  • 相关阅读:
    31. Ubuntu15.04系统中如何启用、禁用客人会话
    dpkg安装deb缺少依赖包的解决方法
    C语言宏中"#"和"##"的用法
    编译android6.0错误recipe for target 'out/host/linux-x86/obj/lib/libart.so' failed
    Android api level对照表
    Android 如何判断CPU是32位还是64位
    vim map nmap(转)
    vim配置及插件安装管理(超级详细)
    Java多线程总结(二)锁、线程池
    Python快速教程目录(转)
  • 原文地址:https://www.cnblogs.com/zcg1051980588/p/12066114.html
Copyright © 2011-2022 走看看