码迷,mamicode.com
首页 > 其他好文 > 详细

剑指offer 21-25

时间:2020-05-26 18:09:54      阅读:56      评论:0      收藏:0      [点我收藏+]

标签:integer   需要   bool   list   util   搜索   code   font   for   

21 从上往下打印二叉树

  从上往下打印出二叉树的每个节点,同层节点从左至右打印。

用一个队列来存储这些曾经访问过但是仍旧有用的节点

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class printFromSameLayer_21 {

    public static void main(String[] args) {
        printFromSameLayer_21 print = new printFromSameLayer_21();
        TreeNode4 root = new TreeNode4(8);
        TreeNode4 node1 = new TreeNode4(6);
        TreeNode4 node2 = new TreeNode4(10);
        TreeNode4 node3 = new TreeNode4(5);
        TreeNode4 node4 = new TreeNode4(7);
        TreeNode4 node5 = new TreeNode4(9);
        TreeNode4 node6 = new TreeNode4(11);
        root.left = node1;
        root.right = node2;
        node1.left = node3;
        node1.right = node4;
        node2.left = node5;
        node2.right = node6;
        ArrayList<Integer> list = print.PrintFromTopToBottom(root);
        System.out.println(list.toString());
    }
    public ArrayList<Integer> PrintFromTopToBottom(TreeNode4 root) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        Queue<TreeNode4> queue = new LinkedList<>();
        queue.add(root);
        int len = queue.size();
        while(!queue.isEmpty()){
            TreeNode4 temp = queue.poll();
            list.add(temp.val);
            if(temp.left != null) queue.add(temp.left);
            if(temp.right != null) queue.add(temp.right);
        }
        return list;
    }

}
class TreeNode4{
    int val;
    TreeNode4 left;
    TreeNode4 right;
    public TreeNode4(int val) {
        this.val = val;
    }
    public void preOrder() {
        System.out.print(this.val+" ");
        if(this.left != null) {
            this.left.preOrder();
        }
        if(this.right != null) {
            this.right.preOrder();
        }
    }
}

22 二叉搜索树的后序遍历序列

  输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同

分析

只需要不断地确定出左子树区间和右子树区间,并且判断:左子树区间的所有结点值 < 根结点值 < 右子树区间所有结点值,这个条件是否满足即可

public class binarySearchTree_22 {

    public static void main(String[] args) {
        int[] sequence = {5,7,6,9,11,10,8};
        binarySearchTree_22 search = new binarySearchTree_22();
        boolean res = search.VerifySquenceOfBST(sequence);
        System.out.println(res);
    }
     public boolean judge(int [] sequence,int start,int end) {
         if(start > end) {
             return true;
         }
         int root = sequence[end];
         int i;
         for(i = 0; i < end;i++) {
             if(sequence[i] > root) {
                 break;
             }
         }
         for(int j = i+1;j < end; j++) {
             if(sequence[j] < root) {
                 return false;
             }
         }
         return judge(sequence,0,i-1) && judge(sequence,i,end-1);
     }
     
     public boolean VerifySquenceOfBST(int [] sequence) {
         if(sequence.length == 0)return false;
         return judge(sequence,0,sequence.length-1);
     }
}

23 二叉树中和为某一值路径

  输入一颗二叉树的根节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。

import java.util.ArrayList;

public class findPath_23 {

    public static void main(String[] args) {
        findPath_23 find = new findPath_23();
        TreeNode5 root = new TreeNode5(10);
        TreeNode5 node1 = new TreeNode5(5);
        TreeNode5 node2 = new TreeNode5(1);
        TreeNode5 node3 = new TreeNode5(2);
        TreeNode5 node4 = new TreeNode5(4);
        TreeNode5 node5 = new TreeNode5(6);
        root.left = node1;
        root.right = node2;
        node1.left = node3;
        node1.right = node4;
        node2.left = node5;
        ArrayList<ArrayList<Integer>> res = find.FindPath(root,17);
        System.out.println(res.toString());
    }

    ArrayList<ArrayList<Integer>> list2 = new ArrayList<ArrayList<Integer>>();
    ArrayList<Integer> list1 = new ArrayList<Integer>();

    public ArrayList<ArrayList<Integer>> FindPath(TreeNode5 root, int target) {
        if(root == null) {
            return list2;
        }
        list1.add(root.val);
        target -= root.val;
        if(root.left == null && root.right == null) {
            if(target == 0) {
                list2.add(new ArrayList<Integer>(list1));
            }
        }
        FindPath(root.left,target);
        FindPath(root.right,target);
        list1.remove(list1.size()-1);
        return list2;
    }
}
class TreeNode5{
    int val;
    TreeNode5 left;
    TreeNode5 right;
    public TreeNode5(int val) {
        this.val = val;
    }
}

 

剑指offer 21-25

标签:integer   需要   bool   list   util   搜索   code   font   for   

原文地址:https://www.cnblogs.com/lgh544/p/12967003.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!