算法题---二叉树篇

1 问题:打印二叉树所有的路径 

给一个二叉树,把所有的路径都打印出来。

比如,对于下面这个二叉树,它所有的路径为:

8 -> 3 -> 1

8 -> 2 -> 6 -> 4

8 -> 3 -> 6 -> 7

8 -> 10 -> 14 -> 13

思路:

从根节点开始,把自己的值放在一个数组里,然后把这个数组传给它的子节点,子节点同样把自己的值放在这个数组里,又传给自己的子节点,直到这个节点是叶节点,然后把这个数组打印出来。所以,我们这里要用到递归。

代码:

[java]  view plain copy
  1. /** 
  2. Given a binary tree, prints out all of its root-to-leaf 
  3. paths, one per line. Uses a recursive helper to do the work. 
  4. */  
  5. //创建将要打印的数组
  6. public void printPaths(Node root, int n) {  
  7.     String[] path = new String[n];  
  8.     printPaths(root, path, 0);  
  9. }  
  10. /** 
  11. Recursive printPaths helper -- given a node, and an array containing 
  12. the path from the root node up to but not including this node, 
  13. prints out all the root-leaf paths. 
  14. */  
  15. private void printPaths(Node node, String[] path, int pathLen) {  
  16.     if (node == nullreturn;  
  17.     // append this node to the path array   // 往要打印的数组中添加值
  18.         path[pathLen++] = node.value;  
  19.     // it's a leaf, so print the path that led to here  
  20. // 正常情况下唯一的出口
  21.     if (node.leftChild == null && node.rightChild == null) {  
  22.         printArray(path, pathLen);  
  23.     }  
  24.     else {  
  25.         // otherwise try both subtrees  
  26.         printPaths(node.leftChild, path, pathLen);  
  27.         printPaths(node.rightChild, path, pathLen);  
  28.     }  
  29. }  
  30. /** 
  31. Utility that prints strings from an array on one line. 
  32. */ 打印出路径
  33. private void printArray(String[] ints, int len) {  
  34.     for (int i = 0; i < len; i++) {  
  35.         System.out.print(ints[i] + " ");  
  36.     }  
  37.     System.out.println();  
  38. }  

备注:这里只能用一个数组+一个数值才能打印出所需要的路径,如果用linkedlist之类的链表结构是不行的。值得分析一下原因,很有意思。

2  检查一个数组是否可能是一棵二叉查询树后序遍历的结果

问题:

给一个数组,检查它是否可能是一棵二叉查询树后序遍历的结果。换句话说,是否存在一棵二叉查询树,对它进行后序遍历后,得到的数组和给定的数组完全一样。


比如,对上面这个二叉查询树后序遍历后,得到的数组是:[1, 4, 7, 6, 3, 13, 14, 10, 8]。

思路:

因为二叉树后序遍历的时候,先左后右然后中间。所以,数组的最后一个一定是整个二叉查询树的根(比如 8),而且,数组前一部分比数组最后一个值(也就是二叉查询树的根)小,因为它们是根的左子树部分。数组剩余部分,是根的右子树部分。而且,在剩余部分里,所有的值都必须比根要大。这样,我们可以把原来的数组分成两个部分,然后每一个部分可以进行递归判断,直到数组的长度小于等于2。(因为当长度小于等于2时,什么样的情况都是可以的)。

我们先把给定的数组分成两个部分,即我们需要先找到数组的分界点。代码如下:

[java]  view plain copy
  1. /** 
  2.      *  
  3.      * @param array  the input array     
  4.      * @param begin  the starting index of the array 
  5.      * @param end    the ending index of the array 
  6.      * @return   the turning point of the array, where the left part of turning point is smaller than array[end], the right  
  7.      *           part of the turning point is larger than array[end]. 
  8.      */  
  9.     public int turningPoint(int[] array, int begin, int end) {  
  10.           
  11.         int tp = -1;  
  12.         for (int i = begin; i < end; i++) {  
  13.             if (array[i] > array[end]) {    // 找到第一个大于根节点的右子树节点
  14.                 tp = i;  
  15.                 break;  
  16.             }  
  17.         }  
  18.           
  19.         return tp;  
  20.     }  

找到分界点后,我们还要判断从分界点开始,到数组末,是否所有的值都比数组的最后一个值(也就是根)大,否则,这样的数组不是一个二叉查询树的后序遍历。代码如下:

[java]  view plain copy
  1. //check whether all the elements from strat are larger than array[end]  
  2.     public boolean checkValid(int[] array, int start, int end) {  
  3.         for (int i = start; i < end; i++) {  
  4.             if (array[i] < array[end]) return false;  
  5.         }  
  6.         return true;  
  7.     }  

有了上面两个方法,我们就可以把递归写出来了,代码如下:


[java]  view plain copy
  1. // the main method to check whether the array is identical to a BST's post order array  
  2.     public boolean isPostOrder(int[] array, int begin, int end) {  
  3.         // the exit  
  4.         if ((end - begin + 1) <= 2return true;  

  5.         //get the turning point  
  6.         int turningPoint = turningPoint(array, begin, end);  
  7.         boolean result = checkValid(array, turningPoint, end);  //check whether the right part of the array is valid  
  8.         if (result == false) {  
  9.             return false;  
  10.         }  
  11.         // recursion   迭代
  12.         return isPostOrder(array, begin, turningPoint - 1) && isPostOrder(array, turningPoint, end - 1);  
  13.     }  

参考 :http://blog.youkuaiyun.com/beiyeqingteng


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值