剑指offer 21-25

Posted lgh544

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了剑指offer 21-25相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode810. 黑板异或游戏/455. 分发饼干/剑指Offer 53 - I. 在排序数组中查找数字 I/53 - II. 0~n-1中缺失的数字/54. 二叉搜索树的第k大节点(代码片段

剑指 Offer(第 2 版)完整题解笔记 & C++代码实现(LeetCode版)

剑指 Offer(第 2 版)完整题解笔记 & C++代码实现(LeetCode版)

LeetCode(剑指 Offer)- 14- I. 剪绳子

LeetCode(剑指 Offer)- 14- I. 剪绳子

剑指offer——第二十九天(动态规划“困难”)