[Leetcode]Binary Tree Inorder Traversal
来源:互联网 发布:cs1.6 fps优化 编辑:程序博客网 时间:2024/06/11 04:38
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1 \ 2 / 3
return [1,3,2]
.
Note: Recursive solution is trivial, could you do it iteratively?
class Solution {public: /*algorithm: recuisvie */ void inorderSub(TreeNode* root,vector<int>&path){ if(!root)return; inorderSub(root->left,path); path.push_back(root->val); inorderSub(root->right,path); } vector<int> inorderTraversal(TreeNode* root) { vector<int>path; inorderSub(root,path); return path; }};
class Solution {public: /*algorithm: iterative */ vector<int> inorderTraversal(TreeNode* root) { vector<int>path; if(!root)return path; stack<TreeNode*>stk; unordered_set<TreeNode*>S; stk.push(root); while(!stk.empty()){ TreeNode* t = stk.top(); while(t->left && !S.count(t->left)){ stk.push(t->left); S.insert(t->left); t = t->left; } path.push_back(t->val); stk.pop(); if(t->right)stk.push(t->right); } return path; }};
0 0
- LeetCode: Binary Tree Inorder Traversal
- [LeetCode]Binary Tree Inorder Traversal
- LeetCode Binary Tree Inorder Traversal
- [Leetcode] Binary Tree Inorder Traversal
- Leetcode: Binary Tree Inorder Traversal
- LeetCode Binary Tree Inorder Traversal
- LeetCode : Binary Tree Inorder Traversal
- [Leetcode] Binary Tree Inorder Traversal
- 【leetcode】Binary Tree Inorder Traversal
- [LeetCode]Binary Tree Inorder Traversal
- Leetcode: Binary Tree Inorder Traversal
- [leetcode]Binary Tree Inorder Traversal
- LeetCode-Binary Tree Inorder Traversal
- Leetcode Binary Tree Inorder Traversal
- [leetcode] Binary Tree Inorder Traversal
- LeetCode - Binary Tree Inorder Traversal
- [LeetCode] Binary Tree Inorder Traversal
- LeetCode | Binary Tree Inorder Traversal
- hdu 1002(简单高精度)
- 解决App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure。
- OCP-V13-680
- oracle 命令创建表空间以及用户、授权
- 第三周项目4 - 顺序表应用
- [Leetcode]Binary Tree Inorder Traversal
- apache kafka系列之在zookeeper中存储结构
- 编程练习——把含有0的行和列清零
- MySql数据库的配置使用
- c++primer plus第十二章-再谈new定位运算符和返回对象const的引用
- POJ 1159Palindrome(区间dp)
- 第三周项目1 顺序表的基本运算
- redis 的两种持久化方式及原理
- Android 代码混淆