标签:遍历 style color class leetcode 二叉树 led solution void
返回排序二叉树第K小的数
还是用先序遍历,记录index和K进行比较
class Solution { public: void helper(TreeNode* node, int& idx, int k, int& res){ if(res!=INT_MAX) return; if(!node) return; //a(node) //lk("root",node) //dsp helper(node->left, idx, k, res); if(idx==k){ res=node->val; //dsp } ++idx; helper(node->right, idx, k, res); } int kthSmallest(TreeNode* root, int k) { int idx=1; int res=INT_MAX; //ahd(root) //a(res) //a(k) //a(idx) helper(root, idx, k, res); return res; } };
程序运行动态演示 http://simpledsp.com/FS/Html/lc230.html
LeetCode 230. Kth Smallest Element in a BST 动态演示
标签:遍历 style color class leetcode 二叉树 led solution void
原文地址:https://www.cnblogs.com/leetcoder/p/11343581.html