zoukankan      html  css  js  c++  java
  • 每日一题 为了工作 2020 0407 第三十六题

    /**
     * 
     * 问题:利用递归方式实现二叉树先序遍历
     * 	   先序遍历 根 左 右
     * @author 雪瞳
     *
     */
    

      

    public class Node {
    	
    	public int value;
    	public Node left;
    	public Node right;
    	public Node(int data){
    		this.value=data;
    	}
    }
    

      

    public class PreOrderRecur {
    
    	public void preOrderRecur(Node head){
    		Node current = head;
    		if(current == null){
    			return;
    		}
    		System.out.print(head.value+"	");
    		preOrderRecur(current.left);
    		preOrderRecur(current.right);
    	}
    }
    

      

    public class TestOrder {
    
    	public static void main(String[] args) {
    		
    		Node root = new Node(1);
    		Node left = new Node(2);
    		Node right = new Node(3);
    		Node left2 = new Node(4);
    		Node left3 = new Node(5);
    		Node left4 = new Node(6);
    		Node right2 = new Node(7);
    		/**
    		 * 
    		 * 					1 
    		 *  			2		3
    		 * 			4
    		 *  	5
    		 *  6       7
    		 */
    		root.left=left;
    		root.right=right;
    		left.left=left2;
    		left2.left=left3;
    		left3.left=left4;
    		left3.right=right2;
    		
    		PreOrderRecur por = new PreOrderRecur();
    		por.preOrderRecur(root);
    	}
    }
    

      

    * 运行结果

     

  • 相关阅读:
    MySQL教程22-字符串类型
    MySQL教程21-日期和时间类型
    MySQL教程20-小数类型
    MySQL教程19-整数类型
    MySQL教程18-数据类型简介
    ActiveMQ_topic
    ActiveMQ_消费者编码
    ActiveMQ_生产者编码
    ActiveMQ介绍
    管理docker容器
  • 原文地址:https://www.cnblogs.com/walxt/p/12654021.html
Copyright © 2011-2022 走看看