java用递归法求斐波那契数列,二叉树常考算法设计题

  java用递归法求斐波那契数列,二叉树常考算法设计题

  

题目一

 

  解法

  class Solution { public int fib(int n){ int[]arr=new int[31];arr[0]=0;arr[1]=1;for(int I=2;I=n;I){ arr[I]=arr[I-2]arr[I-1];} return arr[n];}}

题目二

 

  解法

  /** *二叉树节点的定义。*公共类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解决方案{ int index=0;int ans=0;public int k th small(TreeNode root,int k) { method(root,k);返回ans}无效方法(TreeNode root,int k){ if(root==null)返回;方法(root.left,k);指数;if(index==k){ ans=root。val返回;}方法(root.right,k);}}

题目三

 

  解法

  /** *二叉树节点的定义。*公共类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 int minDepth(TreeNode root){ if(root==null){ return 0;}如果(根。left==空根。right==null){ return 1;} int min_depth=Integer .MAX _ VALUEif (root.left!=null){ min _ depth=math。min(minDepth(root。left)、min _ depth);} if (root.right!=null){ min _ depth=math。min(minDepth(root。右),min _ depth);}返回min _ depth 1;}}到此这篇关于剑指提供之爪哇算法习题精讲二叉树与斐波那契函数的文章就介绍到这了,更多相关爪哇二叉树与斐波那契函数内容请搜索盛行信息技术以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行它!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: