LeetCode之Binary Tree Inorder Traversal
来源:互联网 发布:华为手机今年消费数据 编辑:程序博客网 时间:2024/05/17 23:13
/*方法一:递归中序遍历。*/class Solution {public: vector<int> inorderTraversal(TreeNode* root) { vector<int> res; inorder(root, res); return res; } void inorder(TreeNode* root, vector<int> &res){ if(root == nullptr) return; if(root->left != nullptr) inorder(root->left, res); res.push_back(root->val); if(root->right != nullptr) inorder(root->right, res); }}; /*方法二:利用栈,迭代法中序遍历*/class Solution {public: vector<int> inorderTraversal(TreeNode* root) { vector<int> res; if(root == nullptr) return res; stack<TreeNode*> s; TreeNode *p = root; while(!s.empty() || p != nullptr){ if(p != nullptr){ s.push(p); p = p->left; } else{ p = s.top(); s.pop(); res.push_back(p->val); p = p->right; } } return res; }};
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
- html基础
- ios基础-字符串和数组的block遍历
- android 自定义ScrollView实现背景图片伸缩(阻尼效果)
- 杭电acm 2504又见GCD
- linux epoll事件模型详解(转载)
- LeetCode之Binary Tree Inorder Traversal
- 【UVA10405】【裸LCS】
- CodeForces 5C Longest Regular Bracket Sequence
- android 读取本地大图片时防止oom方法
- python中的字典详细介绍
- Java字符串相关--String和StringBuffer
- IntelliSense: 后面有“::”的名称一定是类名或命名空间名
- 设置secureCRT中vim的字体颜色
- Multiplying by Rotation