leetcode算法题解(Java版)-12-中序遍历

日子又恢复正常了,浪了半个月。。。
还是学习的时候感觉好~~

一、动态规划

题目描述

Given n, how many structurally unique BST's (binary search trees) that store values 1...n?
For example,
Given n = 3, there are a total of 5 unique BST's.

   1         3     3      2      1
    \       /     /      / \      \
     3     2     1      1   3      2
    /     /       \                 \
   2     1         2                 3

思路

  • 题目看上去像是二叉搜索树的题,实际上是动态规划。给到1~n的数,要找出多少种二叉查找树,对于取值为k的数来说,在它左边的又1~k-1,右边的有k+1~n.所以可以把左子树排列的种数乘右子树的种数得到以这个为根的二叉查找树的个数。
  • 用一个状态数组记录下值。

代码

public class Solution {
    public int numTrees(int n) {
        if(n==0){
            return 0;
        }
        int [] f = new int[n+1];
        f[0]=1;
        for(int i=1;i<=n;i++){//外循环,刷新1,2,3,4.。。n的结果
            for(int j=1;j<=i;j++){//小循环,计算各个的值
                f[i]+=f[j-1]*f[i-j];
            }
        }
        return f[n];
    }
}

二、中序遍历

题目描述

Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree{1,#,2,3},

   1
    \
     2
    /
   3

return[1,3,2].

思路

  • 二叉树的中序遍历,就是所谓的左-中-右。
  • 递归和非递归方法,直接看代码!

代码

//递归
/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
import java.util.ArrayList;

public class Solution {
    public ArrayList<Integer> inorderTraversal(TreeNode root) {
        ArrayList<Integer>  res=new ArrayList<Integer>();
        if(root==null)return res;
        inorder(root,res);
        return res;
    }
     public static void inorder(TreeNode root, ArrayList<Integer> list){
        if(root != null){
            inorder(root.left,list);
            list.add(root.val);
            inorder(root.right,list);
        }
    }
}
//非递归
/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

import java.util.ArrayList;
import java.util.Stack;

public class Solution {
    public ArrayList<Integer> inorderTraversal(TreeNode root) {
        ArrayList<Integer> res = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        
        TreeNode node = root;
        if(root==null){
            return res;
        }
        while(!stack.isEmpty()||node!=null){
            while(node!=null){
                stack.add(node);
                node = node.left;
            }
            node = stack.pop();
            res.add(node.val);
            node = node.right;
        }
        return res;
    }
}

三、深搜

题目描述

Given a string containing only digits, restore it by returning all possible valid IP address combinations.
For example:
Given"25525511135",
return["255.255.11.135", "255.255.111.35"]. (Order does not matter)

思路

  • 深度搜索+回溯的时候剪枝

代码

import java.util.ArrayList;

public class Solution {
    public ArrayList<String> restoreIpAddresses(String s) {
        ArrayList<String> res =new  ArrayList<String>();
        ArrayList<String> ip =new ArrayList<String>();
        int start = 0 ;
        dfs(s,res,ip,start);
        return res;
    }
    
    public void dfs(String s,ArrayList<String> res,ArrayList<String> ip,int start){
        if(ip.size()==4&&start==s.length()){
            res.add(ip.get(0)+'.'+ip.get(1)+'.'+ip.get(2)+'.'+ip.get(3));
        }
        
        
        if(s.length()-start > 3*(4-ip.size())){//剪枝
            return ;
        }
        if(s.length()-start+1 < 4-ip.size()){//剪枝
            return ;
        }
        int num = 0 ;
        for(int i=start;i<start+3&&i<s.length();i++){
            num = num*10+(s.charAt(i)-'0');
            if(num<0||num>255){
                return ;
            }
            ip.add(s.substring(start,i+1));
            dfs(s,res,ip,i+1);
            ip.remove(ip.size()-1);
            if(num==0){//可以添加0,但不允许有前缀为0的
                break;
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值