解法一:非递归

 vector<int> preorderTraversal(TreeNode* root)
{
vector<int> res;
if (root == NULL)
return res; stack<TreeNode*> node_stack;
TreeNode *p = root;
while (p || !node_stack.empty()) {
if (p) {
res.push_back(p->val);
node_stack.push(p);
p = p->left;
} else {
p = node_stack.top();
node_stack.pop();
p = p->right;
}
}
return res;
}

解法二:递归

 void preorderTraversalSub(TreeNode* root, vector<int> &res)
{
if (root == NULL)
return;
res.push_back(root->val);
preorderTraversalSub(root->left, res);
preorderTraversalSub(root->right, res);
} vector<int> preorderTraversal(TreeNode* root)
{
vector<int> res;
preorderTraversalSub(root, res);
return res;
}

【LeetCode 144_二叉树_遍历】Binary Tree Preorder Traversal的更多相关文章

  1. [Leetcode 144]二叉树前序遍历Binary Tree Preorder Traversal

    [题目] Given a binary tree, return the preordertraversal of its nodes' values. Example: Input: [1,null ...

  2. LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)

    144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历. LeetCode144. Binary Tree ...

  3. [Swift]LeetCode144. 二叉树的前序遍历 | Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...

  4. 【Leetcode】【Medium】Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  5. 【leetcode刷题笔记】Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  6. C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)

    144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...

  7. 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator

    144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

  8. Binary Tree Preorder Traversal on LeetCode in Java

    二叉树的非递归前序遍历,大抵是很多人信手拈来.不屑一顾的题目罢.然而因为本人记性不好.基础太差的缘故,做这道题的时候居然自己琢磨出了一种解法,虽然谈不上创新,但简单一搜也未发现雷同,权且记录,希望于人 ...

  9. 【LeetCode】Binary Tree Preorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

随机推荐

  1. loki之内存池SmallObj[原创]

    loki库之内存池SmallObj 介绍 loki库的内存池实现主要在文件smallobj中,顾名思义它的优势主要在小对象的分配与释放上,loki库是基于策略的方法实现的,简单的说就是把某个类通过模板 ...

  2. DataFrames与RDDs的相互转换

    Spark SQL支持两种RDDs转换为DataFrames的方式 使用反射获取RDD内的Schema     当已知类的Schema的时候,使用这种基于反射的方法会让代码更加简洁而且效果也很好. 通 ...

  3. 使用distcp并行拷贝大数据文件

    以前我们介绍的访问HDFS的方法都是单线程的,Hadoop中有一个工具可以让我们并行的拷贝大量数据文件,这个工具就是distcp. distcp的典型应用就是在两个HDFS集群中拷贝文件,如果两个集群 ...

  4. POJ1523:SPF(无向连通图求割点)

    题目:http://poj.org/problem?id=1523 题目解析: 注意题目输入输入,防止PE,题目就是求割点,并问割点将这个连通图分成了几个子图,算是模版题吧. #include < ...

  5. try...cath...finally中的return什么时候执行

    一finally可以没有,也可以只有一个.无论有没有发生异常,它总会在这个异常处理结构的最后运行.即使你在try块内用return返回了,在返回前,finally总是要执行,这以便让你有机会能够在异常 ...

  6. 028-B+树(一)

    B+ 树 这部分主要学习:什么是B+树? 了解了 B 树后再来了解下它的变形版:B+ 树,它比 B 树的查询性能更高. 一棵 B+ 树需要满足以下条件: 节点的子树数和关键字数相同(B 树是关键字数比 ...

  7. ListView的ScrollBar设置

    默认ListView的滑动时,右侧会有滑动条显示,等ListView滑动结束时,滑动条消失.修改ScrollBar的显示可以在XML以及CODE中实现. CODE中实现:1.setFastScroll ...

  8. QML基本可视化元素--Text

    一个Text项目可以显示纯文本或者富文本 1.     可以使用Html标记:text: “<b>HELLO</b>” 2.     宽度和高度(width, height): ...

  9. 项目中使用protobuf 3.0

    protocol buffer从3.0 原生的compiler支持c++,Java,Python,Go,Ruby,JavaNano,JavaScript,Objective-C,C#,PHP这篇文章作 ...

  10. ubuntu18.04下监视显卡的运行情况【学习笔记】

    作者:庄泽彬(欢迎转载,请注明作者) 说明:使用watch命令监听显卡的使用 安装完显卡驱动之后系统会生成nvidia-smi 这个工具,我们只需要配合watch命令就可以周期性的查看显卡的信息,-n ...