*LeetCode-Kth Smallest Element in a BST
来源:互联网 发布:数学符号输入软件 编辑:程序博客网 时间:2024/06/05 09:29
一个函数来数这个root下一共多少node 非常简单 recursive
然后就是主函数中判断left有多少node的时候需要数右边 想清楚 +1 -1的事情 因为要算上root本身
public class Solution { public int kthSmallest(TreeNode root, int k) { if ( countNode ( root.left ) < k - 1) return kthSmallest( root.right, k - 1 - countNode( root.left) ); else if ( countNode ( root.left ) >= k) return kthSmallest( root.left, k); else return root.val; } public int countNode ( TreeNode node ){ if ( node == null ) return 0; return 1 + countNode ( node.left ) + countNode ( node.right ); }}
0 0
- #leetcode#Kth Smallest Element in a BST
- leetcode--Kth Smallest Element in a BST
- Leetcode|Kth Smallest Element in a BST
- [leetcode] Kth Smallest Element in a BST
- 【LeetCode】Kth Smallest Element in a BST
- LeetCode Kth Smallest Element in a BST
- 【leetcode】Kth Smallest Element in a BST
- LeetCode Kth Smallest Element in a BST
- leetcode: Kth Smallest Element in a BST
- Leetcode: Kth Smallest Element in a BST
- [LeetCode] Kth Smallest Element in a BST
- [LeetCode]Kth Smallest Element in a BST
- [Leetcode]Kth Smallest Element in a BST
- leetcode--Kth Smallest Element in a BST
- 【leetcode】Kth Smallest Element in a BST
- [leetcode] Kth Smallest Element in a BST
- [Leetcode]Kth Smallest Element in a BST
- Leetcode: Kth Smallest Element in a BST
- cocos2dx and c++ 的小坑
- 常用DIV+CSS命名大全集合
- Xcode出现may cause a leak的解决
- PHP优化之mysql结构
- android第一个用户进程Init进程
- *LeetCode-Kth Smallest Element in a BST
- mac .bash_profile etc...
- C++ new和delete重载
- android中tesseract-ocr的介绍
- 静态库、动态链接库详解
- 【UML】面向对象技术
- cocos-js如何播放cocos studio 上的动画帧
- Training ------ Building Your First App ------ Creating An Android Project
- PSI table 如何在TS 中传输的