Leetcode: binary-tree-preorder-traversal(非递归版)
来源:互联网 发布:php 用pack打包数据包 编辑:程序博客网 时间:2024/06/05 13:23
Leetcode: binary-tree-preorder-traversal
Given a binarytree, return the preorder traversal of itsnodes' values.
思路:运用栈,根->左->右,先遍历左,那就需要先把右节点入栈,再将左节点入栈,这样出棧的时候,才能左节点先出,后出右节点。
代码如下:
vector<int> preorderTraversal(TreeNode *root) { vector<int> res; stack<TreeNode *> node_stack; if(root!=NULL) { node_stack.push(root); while(!node_stack.empty()) { TreeNode *temp = node_stack.top(); node_stack.pop(); res.push_back(temp->val); if(temp->right) node_stack.push(temp->right); if(temp->left) node_stack.push(temp->left); } } return res; }
阅读全文
0 0
- Leetcode: binary-tree-preorder-traversal(非递归版)
- 【Leetcode】144. Binary Tree Preorder Traversal 【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 144. Binary Tree Preorder Traversal-前序遍历|递归|非递归
- 【leetcode】非递归先序遍历二叉树(Binary Tree Preorder Traversal)
- LeetCode 144. Binary Tree Preorder Traversal (前序遍历的非递归实现)
- LeetCode:Binary Tree Preorder Traversal(非递归方法前序遍历二叉树)
- [LeetCode] Binary Tree Preorder Traversal (非递归的先序遍历)
- 【LeetCode】144. Binary Tree Preorder Traversal 二叉树先序遍历的非递归实现
- LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
- LeetCode Binary Tree Preorder Traversal
- 【LeetCode】Binary Tree Preorder Traversal
- Leetcode: Binary Tree Preorder Traversal
- Leetcode Binary Tree Preorder Traversal
- Ubuntu安装pycurl
- Linux的tomcat下部署solr
- MessagePack编解码功能测试代码补齐
- python的类变量与实例变量以及__dict__属性
- Android json解析
- Leetcode: binary-tree-preorder-traversal(非递归版)
- 【每天学一点】如何高效工作,进行项目管理
- R-CNN理解
- JDBC初识
- Tomcat部署方式
- 线性表类型定义与顺序表操作
- nginx代理视频播放响应时间长问题
- angularjs 删除 查询 添加
- 使用eclipse创建maven项目,spring4 mvc