[LC] 144. Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values.
Example:
Input:[1,null,2,3]
1
\
2
/
3 Output:[1,2,3] Solution 1:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
def preorderTraversal(self, root: TreeNode) -> List[int]:
stack, res = [], []
if root is None:
return res
stack.append(root)
while stack:
cur = stack.pop()
res.append(cur.val)
if cur.right:
stack.append(cur.right)
if cur.left:
stack.append(cur.left)
return res
Solution 2:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) {
return res;
}
Deque<TreeNode> queue = new LinkedList<>();
queue.offerFirst(root);
while (!queue.isEmpty()) {
TreeNode cur = queue.pollFirst();
res.add(cur.val);
if (cur.right != null) {
queue.offerFirst(cur.right);
}
if (cur.left != null) {
queue.offerFirst(cur.left);
}
}
return res;
}
}
[LC] 144. 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 ...
- 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- 【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] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
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 ...
- 144. Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- leetcode 144. Binary Tree Preorder Traversal ----- java
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 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. For example:Given binar ...
随机推荐
- mybatis中foreach collection的三种用法
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index,collection,open,separator,close. ...
- 正则表达式模式修正符 比如/esi
正则表达式模式修正符 比如/esi 作者: 字体:[增加 减小] 类型:转载 下面列出了当前在 PCRE 中可能使用的修正符.括号中是这些修正符的内部 PCRE 名.修正符中的空格和换行被忽略,其它字 ...
- linux 下实用工具
gpm 让linux 纯字符终端具备窗口模式下的鼠标功能 xterm + tmux 支持横向或者纵向切屏的终端 urxvt-unicode 支持中文的终端
- AI 领域与概述
概述 数据分析行业主要的职业发展. 业务:业务分析师.数据产品经理.产品总监 技术:算法师.架构师.研发经理.研发总监 美工:BI工程师 人工智能,是数据分析的子集.人工智能主要包括 语音识别 自然语 ...
- Thread--生产者消费者假死分析
package p_c_allWait; public class ValueObject { public static String value = ""; } package ...
- SEO初步学习之影响网站排名的因素
本文介绍一些比较明显的因素,一些隐藏较深的原因还有待发掘: 1.采集网站内容,即抄袭其他网站的内容. 2.新站上传后建议不要有大的改动. 3.标题频繁修改. 4.大量投放垃圾外链. 5.不做友链,交友 ...
- 加速软件源更新和安装 ubuntu 软件中心
Linux mint 12 修改加速软件源更新和安装 ubuntu 软件中心 由于 linux mint 12 是基于 ubuntu 的,可以使用 ubuntu 的源(Ubuntu 11.10 代号 ...
- Log4Net 使用及组合公共类
好记性不如烂笔头,这次是由衷的感受到了! log4net 是一个很好用的日志记录工具,引用入项目中,如何查看项目内部运行情况,如何快速定位异常信息,好的日志记录能帮很大的忙: log4net 很好用, ...
- Ansible-大保健
一.Ansible大纲 Ansible被红帽收购 1.什么是Ansible 2.Ansible特性\优点 3.Ansible基础架构 控制端\被控端\inventory\ad-hoc\playbook ...
- windows支持apache、mysql、php集成环境推荐wampserver3.2 64位版本
对英文不感冒的同学很容易下载到更新包,而且官方的下载速度很慢,此文件为官方原版下载,现在分享给大家. 链接:https://pan.baidu.com/s/1LYyJi6FddvkQQNrLp4L6W ...