zoukankan      html  css  js  c++  java
  • 使用递归打印二叉树的左视图 java

    使用递归打印二叉树的左视图 java

    package com.li.jinRiTouTiao;
    
    public class PrintLeftView {
    
        static class TreeNode{
            TreeNode left;
            TreeNode right;
            int val;
    
            public TreeNode(int val) {
                this.val = val;
            }
        }
    
        int layer=0;  //定义一个全局变量,最大层数layer,  depth是递归的深度。
        public void printleftVeiw(TreeNode root,int depth) {
    
            if (depth > layer) {   //当深度大于层次时,就打印。  因为递归是先左后右,所以每一层 第一个深度大于层次的节点一定为左视图的节点。
                System.out.println(root.val);
                layer++;
            }
            if (root.left != null) {
                printleftVeiw(root.left,depth+1);
            }
            if (root.right!=null) {
                printleftVeiw(root.right,depth+1);
            }
        }
    
    
        public static void main(String[] args) {
            // 构建二叉树
            TreeNode root = new TreeNode(1);
            root.left = new TreeNode(2);
            root.right = new TreeNode(3);
            root.left.left = new TreeNode(4);
            root.right.left = new TreeNode(5);
            root.right.right = new TreeNode(6);
            root.right.left.left = new TreeNode(7);
            root.right.left.right = new TreeNode(8);
            PrintLeftView test = new PrintLeftView();
            test.printleftVeiw(root,1);
        }
    }
  • 相关阅读:
    JAVA内部类的使用
    JAVA try&&catch
    是时候清除你的僵尸代码了
    修复EXE文件无法打开
    MFC实现播放SWF
    JAVA实现swap
    Java RandomAccessFile用法
    程序员如何做出“不难看”的设计
    JAVA期末考试试题
    java static类
  • 原文地址:https://www.cnblogs.com/liyafei/p/9510030.html
Copyright © 2011-2022 走看看