分享

以前单位同事去一家公司的面试题,居然一道都没做全,看看有没有那么难 - 面试秘籍 - Jo...

 软件团队头目 2008-03-18
第一道. 用java 实现二叉树的左遍历


第二道 用科学计数法实现,输入1234返回1,234,输入1234567返回1,234,567,输入12返回
12

第三道 有N+1个不重复数据,一个长度为N的vector数组,vector存放N+1中的数据(vector无重复数据),找出vector中遗漏的那一个数值(即N+1中没有被vector存放的那个值)

public void getValue(Vector n){
...........................(填写)
}
 
回复:
 
第一题:应该是二叉树的前序遍历吧。
Java代码 复制代码
  1. public class Tree {   
  2.   
  3.     private int data;// 数据节点   
  4.     private Tree left;// 左子树   
  5.     private Tree right;// 右子树   
  6.   
  7.     public Tree(int data) {   
  8.         this.data = data;   
  9.         this.left = null;   
  10.         this.right = null;   
  11.     }   
  12.   
  13.     /**  
  14.      * 创建二叉树,返回根结点  
  15.      *   
  16.      * @param input  
  17.      * @return  
  18.      */  
  19.     public static Tree createTree(int[] input) {   
  20.         Tree root = null;   
  21.         Tree temp = null;   
  22.         for (int i = 0; i < input.length; i++) {   
  23.             // 创建根节点   
  24.             if (root == null) {   
  25.                 root = temp = new Tree(input[i]);   
  26.             } else {   
  27.                 // 回到根结点   
  28.                 temp = root;   
  29.                 // 添加节点   
  30.                 while (temp.data != input[i]) {   
  31.                     if (input[i] <= temp.data) {   
  32.                         if (temp.left != null) {   
  33.                             temp = temp.left;   
  34.                         } else {   
  35.                             temp.left = new Tree(input[i]);   
  36.                         }   
  37.                     } else {   
  38.                         if (temp.right != null) {   
  39.                             temp = temp.right;   
  40.                         } else {   
  41.                             temp.right = new Tree(input[i]);   
  42.                         }   
  43.                     }   
  44.                 }   
  45.             }   
  46.         }   
  47.         return root;   
  48.     }   
  49.   
  50.     /**  
  51.      * 前序遍历  
  52.      *   
  53.      * @param tree  
  54.      */  
  55.     public static void preOrder(Tree tree) {   
  56.         if (tree != null) {   
  57.             System.out.print(tree.data + " ");   
  58.             preOrder(tree.left);   
  59.             preOrder(tree.right);   
  60.         }   
  61.     }   
  62.   
  63.     /**  
  64.      * 中序遍历  
  65.      *   
  66.      * @param tree  
  67.      */  
  68.     public static void midOrder(Tree tree) {   
  69.         if (tree != null) {   
  70.             midOrder(tree.left);   
  71.             System.out.print(tree.data + " ");   
  72.             midOrder(tree.right);   
  73.         }   
  74.     }   
  75.   
  76.     /**  
  77.      * 后序遍历  
  78.      *   
  79.      * @param tree  
  80.      */  
  81.     public static void posOrder(Tree tree) {   
  82.         if (tree != null) {   
  83.             posOrder(tree.left);   
  84.             posOrder(tree.right);   
  85.             System.out.print(tree.data + " ");   
  86.         }   
  87.     }   
  88.   
  89.     /**  
  90.      * @param args  
  91.      */  
  92.     public static void main(String[] args) {   
  93.         int[] input = { 4261357 };   
  94.         Tree tree = createTree(input);   
  95.         System.out.print("前序遍历:");   
  96.         preOrder(tree);   
  97.         System.out.print("\n中序遍历:");   
  98.         midOrder(tree);   
  99.         System.out.print("\n后序遍历:");   
  100.         posOrder(tree);   
  101.     }   
  102. }   
  103.   
  104. 前序遍历:4 2 1 3 6 5 7    
  105. 中序遍历:1 2 3 4 5 6 7    
  106. 后序遍历:1 3 2 5 7 6 4   

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多