leetcode - [7]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]
.
思路:前序遍历,“根,左子树,右子树”
#include <iostream>
#include <vector>
#include <stack>
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) {
vector<int> res;
stack<TreeNode*> s;
if (!root) {
return res;
} s.push(root);
while(!s.empty()) {
TreeNode *p = s.top(); s.pop();
res.push_back(p->val);
if (p->right) {
s.push(p->right);
}
if (p->left) {
s.push(p->left);
}
}
return res;
}
}; int main() {
return ;
}
leetcode - [7]Binary Tree Preorder Traversal的更多相关文章
- 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 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 【LeetCode】Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- [LeetCode 题解]: Binary Tree Preorder Traversal
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a bi ...
- 【leetcode】Binary Tree Preorder Traversal (middle)★
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- Java for LeetCode 144 Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...
- leetcode 144. Binary Tree Preorder Traversal ----- java
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- leetcode 题解:Binary Tree Preorder Traversal (二叉树的先序遍历)
题目: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binar ...
- Java [Leetcode 144]Binary Tree Preorder Traversal
题目描述: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given bin ...
- (二叉树 递归) leetcode 144. Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...
随机推荐
- An enumerable sequence of parameters (arrays, lists, etc) is not allo
环境:dapper asp.net core 出错代码如下: public Task<IEnumerable<dynamic>> GetList(string query, p ...
- Android Studio 解析json文件出现中文乱码解决方法
作为一个Android开发初学者,好不容易找到解决方法,跟大家分享一下, 其实很简单,只要保持服务器上的文件(date2.json)与软件的编码方式一样就行. 我用的Android Studio是ut ...
- Oracle_PL/SQL(8) 动态sql
动态sql0.pl/sql块的限制 不能执行ddl操作(create.drop.alter): 不能执行部分dcl操作(grant.revoke). 1.语法动态sql:在执行时才能确定要执行的sql ...
- python下的MySQL数据库编程
https://www.tutorialspoint.com/python/python_database_access.htm if you need to access an Oracle dat ...
- 超强干货,11个灰常实用的AI设计小技巧!
11个超级实用的AI设计小技巧!涉及到很多的实用操作,纯干货经验总结,灰常值得收藏,赶快转走学起来吧! 编辑:千锋UI设计
- 爬虫初窥day3:BeautifulSoup
信息提取 1.通过Tag对象的属性和方法 #!/usr/bin/python # -*- coding: utf- -*- from urllib.request import urlopen fro ...
- [AI]AI章2 框架比较
深度学习框架比较 神经网络一般包括:训练,测试两大阶段.训练:就是把训练数据(原料)和神经网络模型:如AlexNet.RNN等“倒进” 神经网络训练框架例如cafffe等然后用 CPU或GPU(真火) ...
- windows 与 Linux SOCKET通讯
windows client 端口 // Def_win_client_socket_test.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" ...
- Predict the Winner LT486
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...
- rowspan和colspan的区别粗解
rowspan和colspan是我们初学HTML表格中会在做一些特殊表格中遇到.其常在td中添加. rowspan的作用是指定纵向所跨越单元格的行数. 如下效果. colspan的作用是指定单元格横向 ...