【leetcode刷题笔记】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].
Note: Recursive solution is trivial, could you do it iteratively?
解题:应该是很简单的一道题,纠结了好久T_T
基本思路很简单,用栈模拟就可以了。首先根节点压栈,每次弹出栈顶元素,并把它的值存入返回值向量中。如果它的右子树不为空,就把右子树根节点压栈,左子树不为空也把左子数根节点压栈,注意一定是右左这样的顺序。
主要纠结在两个地方:
1. 结构体指针的初始化:
struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode));
注意要给指针分配空间,使用malloc需要包含头文件#include<cstdlib>
2.每次压栈以后栈顶元素就变了,所以要先把栈顶元素pop到一个变量中,再继续后续操作。
代码如下:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
#include <iostream>
#include <stack>
#include <vector>
#include <cstdlib>
using namespace std;
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) {
stack<TreeNode*>s;
vector<int>ans;
if(root == NULL)
return ans;
s.push(root);
while(!s.empty()){
struct TreeNode* temp = s.top();
s.pop();
ans.push_back(temp->val);
if(temp->right != NULL)
s.push(temp->right);
if(temp->left != NULL){
s.push(temp->left);
}
}
return ans;
}
};
int main(){
Solution so;
struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode));
struct TreeNode* root_l_c = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->val = ;
root_l_c->val = ; root_l_c->left = NULL;
root_l_c->right = NULL; root->right = NULL;
root->left = root_l_c;
//cout << root->left->val<<endl;
so.preorderTraversal(root);
}
Java版本递归解法:
public class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> answer = new ArrayList<>();
return preorderTraversalHelper(answer, root);
}
public List<Integer> preorderTraversalHelper(List<Integer> answer,TreeNode root) {
if(root == null)
return answer;
answer.add(root.val);
answer = preorderTraversalHelper(answer,root.left);
answer = preorderTraversalHelper(answer,root.right);
return answer;
}
}
一个小笔记是在eclipse里面使用List或者ArrayList的时候要加上
import java.util.ArrayList;import java.util.List;
【leetcode刷题笔记】Binary Tree Preorder Traversal的更多相关文章
- [刷题] 144 Binary Tree Preorder Traversal
要求 二叉树的前序遍历 实现 递归 栈模拟 定义结构体 Command 模拟指令,字符串s描述命令,树节点node为指令作用的节点 定义栈 Stack 存储命令 1 #include ...
- 刷题94. Binary Tree Inorder Traversal
一.题目说明 题目94. Binary Tree Inorder Traversal,给一个二叉树,返回中序遍历序列.题目难度是Medium! 二.我的解答 用递归遍历,学过数据结构的应该都可以实现. ...
- LeetCode之“树”:Binary Tree Preorder && Inorder && Postorder Traversal
Binary Tree Preorder Traversal 题目链接 题目要求: Given a binary tree, return the preorder traversal of its ...
- Binary Tree Preorder Traversal on LeetCode in Java
二叉树的非递归前序遍历,大抵是很多人信手拈来.不屑一顾的题目罢.然而因为本人记性不好.基础太差的缘故,做这道题的时候居然自己琢磨出了一种解法,虽然谈不上创新,但简单一搜也未发现雷同,权且记录,希望于人 ...
- 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 ...
- 【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 ...
- LeetCode: Binary Tree Preorder Traversal 解题报告
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal
详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal Given a binary tree, return the po ...
随机推荐
- UINavigationController改变动画效果
@interface UINavigationController (CustomTransition) - (void) pushWithCustomAnimation:(UIViewControl ...
- winform对话框拖拽显示文件路径的问题
allow drop=true; dragEnter dragDrop vs管理员账户拖拽会失败
- Delphi 泛型(三十篇)
Delphi 泛型(三十篇)http://www.cnblogs.com/jxgxy/category/216671.html
- nightwatchJS ---element用法
.element() Search for an element on the page, starting from the document root. The located element w ...
- gopath基础概念
GOROOT golang安装路径. GOPATH 官方解释,请google.go工作环境中常常用到的一个很重要的环境变量(这种设计类似java).具体用途:go命令常常需要用到的,如go run,g ...
- python 中的"switch"用法
转载:http://python.jobbole.com/82008/ 为什么Python中没有Switch/Case语句? 不同于我用过的其它编程语言,Python 没有 switch / case ...
- Python基础之函数与装饰器
阅读目录 一.为什么要使用函数 二.函数的定义与调用 三.函数返回值 四.函数的参数 五.本章小结 六.装饰器 一.函数流程图: 函数名的命名规则: 1.函数名必须由字母下划线数字组成,不能是关键字和 ...
- 在oracle11g中配置多个DataGuard物理备机
>> from zhuhaiqing.info 主机配置 alter system set DB_UNIQUE_NAME='starboss' scope=spfile; alter sy ...
- C++ Primer(第五版)读书笔记 & 习题解答 --- Chapter 1
Chapter 1.1 1. 每个C++程序都必须有且只能有一个main函数,main函数的返回类型必须是int.操作系统通过调用main函数来运行C++程序. 2. 一个函数的定义包含四部分:返回类 ...
- iOS collectionView添加类似tableView的tableHeaderView
我们都知道UITableview有一个tableHeaderFooterView,这样我们在布局页面的时候,如果顶部有轮播图,可以直接把轮播图设置为tableView的HeaderFooterView ...