【二叉树】BST第K小值【230. Kth Smallest Element in a BST】
2017-07-15 12:02
225 查看
题目链接:https://leetcode.com/problems/kth-smallest-element-in-a-bst/#/discuss
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int kthSmallest(TreeNode* root, int k) { return getOrder(root,k); } // 中序遍历 int getOrder(TreeNode* root,int& k){ if(root){ int t=getOrder(root->left,k); if(!k) return t; k--; if(k) t=getOrder(root->right,k); else t=root->val; return t; } return -1; } };
相关文章推荐
- LeetCode-230. Kth Smallest Element in a BST (JAVA)二叉树第k小的数字
- 230. Kth Smallest Element in a BST | 二叉排序树中查找第K小的数
- [LeetCode] 230. Kth Smallest Element in a BST 二叉搜索树中的第K小的元素
- leecode 解题总结:230. Kth Smallest Element in a BST
- LeetCode OJ 230. Kth Smallest Element in a BST
- [LeetCode]230. Kth Smallest Element in a BST
- leetcode 230. Kth Smallest Element in a BST-递归|非递归
- leetcode No230. Kth Smallest Element in a BST
- leetcode 230. Kth Smallest Element in a BST
- [Leetcode] 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- Kth Smallest Element in a BST 寻找二叉排序树中第k小元素
- Leetcode 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST(Java)
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- leetcode 230. Kth Smallest Element in a BST
- [leetcode 230]Kth Smallest Element in a BST----求二叉搜索树的第K小值