剑指Offer之Java算法习题精讲N叉树的遍历及数组与字符串

题目一

剑指Offer之Java算法习题精讲N叉树的遍历及数组与字符串_第1张图片

解法

/*
// Definition for a Node.
class Node {
    public int val;
    public List children;
    public Node() {}
    public Node(int _val) {
        val = _val;
    }
    public Node(int _val, List _children) {
        val = _val;
        children = _children;
    }
};
*/
 
class Solution {
    ArrayList list = new ArrayList();
    public List preorder(Node root) {
        if (root == null) return list;
        list.add(root.val);
        for(int i=0;i 
 

题目二

剑指Offer之Java算法习题精讲N叉树的遍历及数组与字符串_第2张图片

 解法

/*
// Definition for a Node.
class Node {
    public int val;
    public List children;
    public Node() {}
    public Node(int _val) {
        val = _val;
    }
    public Node(int _val, List _children) {
        val = _val;
        children = _children;
    }
};
*/
 
class Solution {
    ArrayList list = new ArrayList();
    public List postorder(Node root) {
        if(root==null) return list;
        for(int i = 0;i 
 

题目三

剑指Offer之Java算法习题精讲N叉树的遍历及数组与字符串_第3张图片

解法

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public String tree2str(TreeNode root) {
        // root为空返回""
        if(root==null){
            return "";
        }
        // root左右为空
        if(root.left==null&&root.right==null){
            return root.val+"";
        }
        // root右为空
        if(root.right==null){
            return root.val+"("+tree2str(root.left)+")";
        }
        // root左右不空或是左不为空
        return root.val+"("+tree2str(root.left)+")"+"("+tree2str(root.right)+")";
    }
}

题目四

剑指Offer之Java算法习题精讲N叉树的遍历及数组与字符串_第4张图片

解法

class Solution {
    public int maximumProduct(int[] nums) {
        Arrays.sort(nums);
        int n = nums.length;
        // 拿到最大三个数,或者最小两个数和最大一个数
        int ans = Math.max(nums[n-1]*nums[n-2]*nums[n-3],nums[n-1]*nums[0]*nums[1]);
        return ans;
    }
}
 
// 拿到最大三个数,或者最小两个数和最大一个数
class Solution {
    public int maximumProduct(int[] nums) {
        // 最小的和第二小的
        int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE;
        // 最大的、第二大的和第三大的
        int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE;
        for(int n:nums){
            if(nmax1){
                max3 = max2;
                max2 = max1;
                max1 = n;
            }else if(n>max2){
                max3 = max2;
                max2 = n;
            }else if(n>max3){
                max3 = n;
            }
        }
        return Math.max(min1*min2*max1,max1*max2*max3);
    }
}

到此这篇关于剑指Offer之Java算法习题精讲N叉树的遍历及数组与字符串的文章就介绍到这了,更多相关Java N叉树的遍历内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(剑指Offer之Java算法习题精讲N叉树的遍历及数组与字符串)