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

剑指offer第三天

时间:2018-01-19 23:25:38      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:模拟   字符   知识点   remove   nod   结果   遍历   array   节点   

21.栈的压入、弹出序列

输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)

【解题思路】:设计一个辅助栈,如果下一个弹出的数字是辅助栈的栈顶,则弹出,如果不是栈顶,则继续将压入序列压入辅助栈,直到把下一个需要弹出的数字压入栈顶为止;如果所有数字都压入辅助站,栈顶仍然不是欲弹出的数字,则该序列不可能是一个弹出序列。

import java.util.Stack;
public class Solution {
    public boolean IsPopOrder(int [] pushA,int [] popA) {
        if(pushA == null||popA == null||pushA.length!=popA.length) return false;
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for(int i = 0 ;i<popA.length;i++){
            //一定注意这里需要先判断一下栈是否为空,
            //如果为空,则调用peek()时会出现异常;
            if(stack.empty())
                stack.push(pushA[j++]);
            while(stack.peek()!=popA[i]&&j<pushA.length)
                stack.push(pushA[j++]);
            if(stack.peek()==popA[i])
                stack.pop();
            else
                return false;
        }
        return true;
    }
}

22.从上往下打印二叉树

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

Java知识点:

  1. 返回长度:
    1. String.length();String字符串用length()方法会返回其长度。
    2. Array.length;数组有length属性,直接数组名点length就可以取到。
    3. ArrayList.size()方法的会返回其长度。
  2. ArrayList 操作:
    get(),add(),remove()
    You need to use the get() method to get the element at a particular index from an ArrayList. You can‘t use [] to get the element at a particular index, in an arraylist. Its possible only for arrays and your files is not an array, but an ArrayList.

/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
import java.util.ArrayList;
/**
用arraylist模拟一个队列来存储相应的TreeNode
*/
public class Solution {
    ArrayList<Integer> result = new ArrayList<>();
    ArrayList<TreeNode> temp = new ArrayList<>();
    public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
        if(root == null) return result;
        temp.add(root);
        while(temp.size() != 0){
            TreeNode node = temp.remove(0);
            result.add(node.val);
            if(node.left!=null)
                temp.add(node.left);
            if(node.right!=null)
                temp.add(node.right);
        }
        return result;
    }
}

23.二叉搜索树的后序遍历序列

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

public class Solution {
    public boolean VerifySquenceOfBST(int [] sequence) {
        if(sequence == null || sequence.length == 0) return false;
        int start = 0,end = sequence.length-1;
        return isSearchTree(sequence,start,end);
    }
    public boolean isSearchTree(int [] sequence,int start,int end){
        if(end==start) return true;
        int root = sequence[end];
        int index = end;
        for(int i = start;i<end;i++){
            if(sequence[i] > root){
                index = i;
                break;
            }
        }
        for(int i = index;i<end;i++)
            if(sequence[i]< root)
                return false;
        if(index == end||index == start)// index = end 时没有右子树;index = start时没有左子树; 
            return isSearchTree(sequence,start,end-1);
        else
            return isSearchTree(sequence,start,index-1)&&isSearchTree(sequence,index,end-1);
    }
}

24.二叉树中和为某一值的路径

输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。
【解题思路】:因为根结点和叶子结点一定在路径中,而且路径开始一定是跟结点,使用前序遍历遍历二叉树,每经过一个结点减小target的值,直至找到使target=0的叶子结点,即为路径,每次回退,需要删除路径中最后一个结点。

import java.util.ArrayList;
/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    private ArrayList<ArrayList<Integer>> allPath = new ArrayList<>();
    private ArrayList<Integer> path = new ArrayList<>();
    public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
        if(root == null) return allPath;
        target -= root.val;
        path.add(root.val);
        if(target == 0&& root.left == null&&root.right == null)
            allPath.add(new ArrayList<Integer>(path));
        else{
            FindPath(root.left,target);
            FindPath(root.right,target);
        }
        path.remove(path.size()-1);
        return allPath;
        
    }
}

剑指offer第三天

标签:模拟   字符   知识点   remove   nod   结果   遍历   array   节点   

原文地址:https://www.cnblogs.com/guoyaohua/p/8319094.html

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