【LeetCode 144_二叉树_遍历】Binary Tree Preorder Traversal

解法一:非递归
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的更多相关文章
- [Leetcode 144]二叉树前序遍历Binary Tree Preorder Traversal
[题目] Given a binary tree, return the preordertraversal of its nodes' values. Example: Input: [1,null ...
- LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)
144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历. LeetCode144. Binary Tree ...
- [Swift]LeetCode144. 二叉树的前序遍历 | Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...
- 【Leetcode】【Medium】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
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...
- 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- 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 ...
随机推荐
- 使用dockerfile 创建ubuntu ssh镜像
############################################################ # Dockerfile to build ubunto ssh contai ...
- Mirror--程序访问镜像数据库的超时机制
程序在访问有镜像的数据库和无镜像的数据库时,采用的链接超时时间算法不一样,因此会导致在在有镜像的数据库上设置了15 S的超时时间,而实际的超时时间仅为3.6 S,从而导致有镜像的数据库更容易超时. 在 ...
- git-【七】bug分支
在开发中,会经常碰到bug问题,那么有了bug就需要修复,在Git中,分支是很强大的,每个bug都可以通过一个临时分支来修复,修复完成后,合并分支,然后将临时的分支删除掉. 比如我在开发中接到一个40 ...
- golang 复制对象的正确做法
需求 实际运用种,传参是一对象指针,现在如何最简便地复制一对象? 实现 坑:&* 先拿到值再指针? package main import ( "time" " ...
- docker——三剑客之Docker Compose
编排(Orchestration)功能是复杂系统实现灵活可操作性的关键.特别是在Docker应用场景中,编排意味着用户可以灵活的对各种容器资源实现定义和管理. 作为Docker官方编排工具,Compo ...
- uva1292 树形dp
这题说的是给了一个n个节点的一棵树,然后 你 从 这 棵 树 的 n 个 节点中 选择 尽量少的 点使得 每条边都至少有一个 士兵看守 dp[0][i]+=dp[1][j] dp[1][i]+=min ...
- C#反射——模仿BeanUtil属性复制
反射工具类请参见:https://www.cnblogs.com/threadj/p/10535796.html using System; using System.Collections.Gene ...
- postman--实现接口自动化测试
postman使用 开发中经常用postman来测试接口,一个简单的注册接口用postman测试: 接口正常工作只是最基本的要求,经常要评估接口性能,进行压力测试. postman进行简单压力测试 下 ...
- 《Java入门第二季》第二章 封装
什么是java中的封装1.封装的概念:隐藏信息.隐藏具体的实现细节. 2.封装的实现步骤: 1)修改属性的可见性,private.2)创建修改器方法和访问器方法,getXXX/setXXX.(未必一定 ...
- 搞定PHP面试 - 正则表达式知识点整理
一.简介 1. 什么是正则表达式 正则表达式(Regular Expression)就是用某种模式去匹配一类字符串的一种公式.正则表达式使用单个字符串来描述.匹配一系列匹配某个句法规则的字符串.正则表 ...