Java中二叉树与斐波那契函数的示例分析
这篇文章主要介绍Java中二叉树与斐波那契函数的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
题目一

解法
classSolution{publicintfib(intn){int[]arr=newint[31];arr[0]=0;arr[1]=1;for(inti=2;i<=n;i++){arr[i]=arr[i-2]+arr[i-1];}returnarr[n];}}
题目二

解法
/***Definitionforabinarytreenode.*publicclassTreeNode{*intval;*TreeNodeleft;*TreeNoderight;*TreeNode(){}*TreeNode(intval){this.val=val;}*TreeNode(intval,TreeNodeleft,TreeNoderight){*this.val=val;*this.left=left;*this.right=right;*}*}*/classSolution{intindex=0;intans=0;publicintkthSmallest(TreeNoderoot,intk){method(root,k);returnans;}voidmethod(TreeNoderoot,intk){if(root==null)return;method(root.left,k);index++;if(index==k){ans=root.val;return;}method(root.right,k);}}
题目三

解法
/***Definitionforabinarytreenode.*publicclassTreeNode{*intval;*TreeNodeleft;*TreeNoderight;*TreeNode(){}*TreeNode(intval){this.val=val;}*TreeNode(intval,TreeNodeleft,TreeNoderight){*this.val=val;*this.left=left;*this.right=right;*}*}*/classSolution{publicintminDepth(TreeNoderoot){if(root==null){return0;}if(root.left==null&&root.right==null){return1;}intmin_depth=Integer.MAX_VALUE;if(root.left!=null){min_depth=Math.min(minDepth(root.left),min_depth);}if(root.right!=null){min_depth=Math.min(minDepth(root.right),min_depth);}returnmin_depth+1;}}
以上是“Java中二叉树与斐波那契函数的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注恰卡编程网行业资讯频道!