LeetCode之Binary Tree Preorder Traversal
来源:互联网 发布:js 鼠标点击后div移动 编辑:程序博客网 时间:2024/05/16 05:22
/*方法一:递归前序遍历。*/class Solution {public: vector<int> preorderTraversal(TreeNode* root) { vector<int> res; preorder(root, res); return res; } void preorder(TreeNode *root, vector<int> &res){ if(root == nullptr) return; res.push_back(root->val); if(root->left) preorder(root->left, res); if(root->right) preorder(root->right, res); }}; /*方法二:利用栈,采用迭代进行前序遍历。*/class Solution {public: vector<int> preorderTraversal(TreeNode* root) {vector<int> res;stack<TreeNode*> s;s.push(root);while(!s.empty()){TreeNode *p = s.top();s.pop();if(p != nullptr){res.push_back(p->val);if(p->right != nullptr) s.push(p->right);if(p->left != nullptr) s.push(p->left);}}return res; }};
0 0
- LeetCode 之 Binary Tree Preorder Traversal
- LeetCode之Binary Tree Preorder Traversal
- leetcode之Binary Tree Preorder Traversal
- LeetCode 之 preorder traversal of binary tree
- LeetCode之Binary Tree Preorder Traversal
- leetcode之Binary Tree Preorder Traversal
- LeetCode 之 Binary Tree Preorder Traversal
- LeetCode Binary Tree Preorder Traversal
- 【LeetCode】Binary Tree Preorder Traversal
- Leetcode: Binary Tree Preorder Traversal
- Leetcode Binary Tree Preorder Traversal
- LeetCode:Binary Tree Preorder Traversal
- Leetcode: Binary Tree Preorder Traversal
- LeetCode:Binary Tree Preorder Traversal
- [LeetCode]Binary Tree Preorder Traversal
- [LeetCode] Binary Tree Preorder Traversal
- LeetCode | Binary Tree Preorder Traversal
- Leetcode: Binary Tree Preorder Traversal
- duilib各种布局的作用,相对布局与绝对布局的的意义与用法
- OpenCV2:等间隔采样和局部均值的图像缩小
- 在MyEclipse中设置jsp页面为默认utf-8编码
- struts2登陆拦截器
- JAVA虚拟机生命周期
- LeetCode之Binary Tree Preorder Traversal
- RGB图像转为灰度图
- float to string
- 删除共享
- spring hibernate 整合+项目框架搭建 包含详细注释(清华大学各系统使用代码)
- Xcode离线帮助文档的安装
- LeetCode(98) Validate Binary Search Tree
- html 图片 地图映射
- 设置MyEclipse开发项目时使用的JDK