【Leetcode】【Medium】Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
return [1,2,3].
解题思路:
二叉树非递归先序遍历,使用栈来保存被遍历到的,但是还没遍历其右子树的结点。
代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> vals;
stack<TreeNode*> nodes;
TreeNode* node = root; while (node != NULL || !nodes.empty()) {
while (node) {
nodes.push(node);
vals.push_back(node->val);
node = node->left;
} node = nodes.top();
nodes.pop();
node = node->right;
} return vals;
}
};
【Leetcode】【Medium】Binary Tree Preorder Traversal的更多相关文章
- [Leetcode 144]二叉树前序遍历Binary Tree Preorder Traversal
[题目] Given a binary tree, return the preordertraversal of its nodes' values. Example: Input: [1,null ...
- 【LeetCode】Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...
- LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)
144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历. LeetCode144. Binary Tree ...
- Binary Tree Preorder Traversal on LeetCode in Java
二叉树的非递归前序遍历,大抵是很多人信手拈来.不屑一顾的题目罢.然而因为本人记性不好.基础太差的缘故,做这道题的时候居然自己琢磨出了一种解法,虽然谈不上创新,但简单一搜也未发现雷同,权且记录,希望于人 ...
- LeetCode: Binary Tree Preorder Traversal 解题报告
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 55. Binary Tree Preorder Traversal
Binary Tree Preorder Traversal My Submissions QuestionEditorial Solution Total Accepted: 119655 Tota ...
- 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal
详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal Given a binary tree, return the po ...
- 3月3日(3) Binary Tree Preorder Traversal
原题 Binary Tree Preorder Traversal 没什么好说的... 二叉树的前序遍历,当然如果我一样忘记了什么是前序遍历的.. 啊啊.. 总之,前序.中序.后序,是按照根的位置来 ...
随机推荐
- Java基础29-子父类中的成员变量
/* 成员: 1.成员变量 2.函数 3.构造函数 变量: this 代表当前对象的引用 this.变量 首先在本类中找所需要的变量,如果没有找到再父类中找. super 用于访问当前对象的父类成员, ...
- Go语言包管理工具Glide
Go语言包管理工具用于管理Go项目的库和库的依赖引用. 包管理工具普遍会在Go项目根目录生成Vendor文件夹并自动下载该Go项目需要的库. Go语言有很多开源的包管理工具,这里主要介绍Glide ...
- 我的Python升级打怪之路【六】:面向对象(一)
面向对象的概述 面向过程:根据业务逻辑从上到下写代码 函数式:将其功能代码封装到函数中,日后便无需编写,仅仅调用即可 [执行函数] 面向对象:对函数进行分类和封装.[创建对象]==>[通过对象执 ...
- git学习笔记5
查看保存的进度 git stash list 恢复进度 git stash pop 测试运行哪些文件会被删除 git clean -nd 强制删除 git clean -fd 保存当前的工作进度,会保 ...
- Android Gson解析复杂Json
JSON原数据 {"total":1,"rows":[{"ID":1,"Title":"台州初级中学招收初一年 ...
- Unity Github 项目收集
http://gad.qq.com/article/detail/24048 重磅推荐: Github 热门 Unity Assets 查询:http://unitylist.com/browse 最 ...
- 在C代码中将结构体变量作为参数传递效率忒低
在C语言编程中,我们几乎不可能看见有人将一个结构体变量作为参数进行传递,因为效率太低了.本文尝试从反汇编的角度给出其中的缘由. 对于C语言来说,所有的参数传递都是值传递.如果一个变量为指针,那么传递的 ...
- Spark中自定义累加器Accumulator
1. 自定义累加器 自定义累加器需要继承AccumulatorParam,实现addInPlace和zero方法. 例1:实现Long类型的累加器 object LongAccumulatorPara ...
- 判断字符串中是否包含Emoji表情代码
判断字符串中是否包含Emoji表情代码: + (BOOL)stringContainsEmoji:(NSString *)string { __block BOOL returnValue = NO; ...
- linux 修改用户主目录(转载)
第一:修改/etc/passwd文件 第二:usermod命令 详细说明如下: 第一种方法: vi /etc/passwd 找到要修改的用户那几行,修改掉即可.此法很暴力,建议慎用. 第二种:user ...