230. Kth Smallest Element in a BST
2016-02-29 14:05
465 查看
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree
return
solution:
心得:利用中序遍历
运行速度较快。
For example:
Given binary tree
{1,#,2,3},
1 \ 2 / 3
return
[1,3,2].
solution:
/** * 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: vector<int> inorderTraversal(TreeNode* root) { if(!root) return {}; vector<int> res; stack<TreeNode *> stk; while(root||!stk.empty()){ while(root){ stk.push(root); root = root->left; } root = stk.top(); stk.pop(); res.push_back(root->val); root = root->right; } return res; } };
心得:利用中序遍历
运行速度较快。
相关文章推荐
- C语言深度剖析-----C语言中的字符串
- android做支付宝移动支付时遇到跳转失败问题
- React-组件的复合
- 8款实用Sublime text 3插件推荐
- come on,make a date progress bar together!
- java利用snmp4j包来读取snmp协议数据
- linux shell(1)
- I9500联通定制版刷cm12.1
- mof格式的文件怎么打开?用什么工具?
- Opencv中的ROI介绍
- 编译安装mysql5.6.29及安装报错的解决方法
- 2010: Hardware Design Flow based on USA company
- Android面试题之Activity
- 一网打尽当下NoSQL类型、适用场景及使用公司
- android:常用的AlertDialog对话框及自定义对话框
- #已解决#如何解决ld: library not found for -lAFNetworking这个问题
- JAVASE eclipse中的命名
- ftok()函数深度解析
- Java编程基础----Java中有哪些数据类型?
- C语言中EOF是什么意思?