二叉樹路徑問題

/**
 *       1
 *     2   3
 *   4  5 6 7
 * 
 * 
 * 先序遍歷:
 *      注意用list存儲時,找出一條路徑時4時:
 *              left=right=null,移除4
 *          爲2時:
 *              left、right遍歷完成,移除2節點
 *          
 */
//二叉樹各個路徑的和
  此方式不能獲取只存在左、右葉子的爲空一邊的路徑
    public static void bTAllPathSum(Node root,int s) {
         if(root==null){
             return;
         }
        s+=root.dat;
        if(root.left==null&&root.right==null) {
            System.out.println(s);
        }
        bTAllPathSum(root.left,s);
        bTAllPathSum(root.right,s);
    }   
//打印所有路徑    
public static void printAllPath(Node root,String s) {
          if(root==null){
                return;
          }
          s+=root.dat+" ";
          if(root.left==null&&root.right==null) {
                System.out.println(
                s.trim().replaceAll(" ","->"));
          }
          printAllPath(root.left,s);
          printAllPath(root.right,s);   
}

綜合以上:

//打印所有何爲aim的路徑
    public static void iterator(Node root,String s,int t,int aim) {
          if(root==null){
                return;
          }
          s+=root.dat+" ";
          t+=root.dat;
          if(root.left==null&&root.right==null) {
              if(t==aim){
                System.out.println(
                s.trim().replaceAll(" ","->"));
              }
          }
          iterator(root.left,s,t,aim);
          iterator(root.right,s,t,aim);
    }   
注:以上搜索路徑時:
             1
           2   3 
         4
        輸出:1 2  4
             1 3
        不會輸出:1   2
//利用二叉樹的非遞歸先序遍歷:求和爲sum的路徑
    public void findPath(Node root,int sum){
         if(root==null){
             return;
         }
         Stack<Node> stack=new Stack<Node>();
         Node head=root; 
         stack.push(head);
         int cur=0;  
         List<Integer> list=new ArrayList<Integer>();
         while(!stack.isEmpty()){
             head=stack.peek();
             cur+=head.dat;
             list.add(head.dat);
             if(head.right!=null){
                 stack.push(head.right);
             }
             if(head.left!=null){
                 stack.push(head.left);
             }
             if(head.left==null&&head.right==null){
                 if(cur==sum){
                     System.out.println(cur);
                     break;
                 }else if(!stack.isEmpty()){
                     Node tmp=stack.pop();
                     cur-=tmp.dat;
                     list.remove(list.size()-1);
                 }
             }
         }
        System.out.println(list);
     }   
//所有路徑
public static List<List<Integer>> pathSum1(Node root,int sum,List<List<Integer>> listAll,List<Integer> list){
        if(root == null) 
            return listAll;  
        list.add(root.dat);    
        if(root.left==null||root.right==null){
            listAll.add(new ArrayList<Integer>(list));  
        }       
        pathSum1(root.left, sum,listAll,list);  
        pathSum1(root.right, sum,listAll,list);  
        list.remove(list.size()-1); 
        return listAll;  
    }
    public static List<List<Integer>> pathSum(Node root,int sum,List<List<Integer>> listAll,List<Integer> list){
        if(root == null) 
            return listAll;  
        list.add(root.dat);  
        sum-=root.dat;  
        if((root.left == null||root.right == null)
        &&sum == 0){
            listAll.add(new ArrayList<Integer>(list));  
        }
        pathSum(root.left, sum,listAll,list);  
        pathSum(root.right, sum,listAll,list);  
        list.remove(list.size()-1); 
        return listAll;  
    }

附:

          public Node build(int[] arr,int i){
        if(i>=arr.length){
            return null;
        }
        Node node=new Node();
        node.dat=arr[i];
        node.left=build(arr, 2*i+1);
        node.right=build(arr, 2*i+2);
        return node;
    }
    public void show(Node head){
        if(head!=null){
            Queue<Node> queue=new LinkedList<Node>();
            queue.add(head);
            while(!queue.isEmpty()){
                head=queue.poll();
                System.out.print(head.dat+" ");
                if(head.left!=null){
                    queue.offer(head.left);
                }
                if(head.right!=null){
                    queue.offer(head.right);
                }
            }
        }
    }
    class Node{
        int dat;
        Node left,right;
    }   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章