[LeetCode]144. Binary Tree Preorder Traversal二叉树前序遍历
关于二叉树的遍历请看:
http://www.cnblogs.com/stAr-1/p/7058262.html
/*
考察基本功的一道题,迭代实现二叉树前序遍历
*/
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root==null)
return res;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty())
{
TreeNode cur = stack.pop();
res.add(cur.val);
if (cur.right!=null)
stack.push(cur.right);
if (cur.left!=null)
stack.push(cur.left);
}
return res;
}
[LeetCode]144. Binary Tree Preorder Traversal二叉树前序遍历的更多相关文章
- 144. 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 ...
- [LeetCode] 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 二叉树的前序遍历 C++
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [,,] \ / Ou ...
- Leetcode 144 Binary Tree Preorder Traversal 二叉树
二叉树的基础操作:二叉树的先序遍历(详细请看数据结构和算法,任意本书都有介绍),即根,左子树,右子树,实现方法中还有用栈实现的,这里不介绍了 /** * Definition for binary t ...
- (二叉树 递归) leetcode 144. Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...
- LeetCode OJ: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(二叉树的前序遍历)
这道题是LeetCode里的第144道题. 题目要求: 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很 ...
- 144 Binary Tree Preorder Traversal 二叉树的前序遍历
给定一棵二叉树,返回其节点值的前序遍历.例如:给定二叉树[1,null,2,3], 1 \ 2 / 3返回 [1,2,3].注意: 递归方法很简单,你可以使用迭代方法来解决 ...
随机推荐
- Python函数的关键字参数
除了位置参数的函数参数使用方式,还有一种在函数调用时指定形参等于指定实参的参数使用模式,该模式称为关键字参数.关键字参数使用可以不按形参的顺序传递实参,系统按形参的名字确认实参传递给哪个参数. 具体内 ...
- python序列化与反序列化(json、pickle)-(五)
1.什么是序列化&反序列化? 序列化:将字典.列表.类的实例对象等内容转换成一个字符串的过程. 反序列化:将一个字符串转换成字典.列表.类的实例对象等内容的过程 PS:Python中常见的数据 ...
- CTF写脚本
今天总结一下CTF如何写脚本快速得分....(比较菜,能力有限,大佬勿喷) 所谓的写脚本得分,就是利用了 python爬虫的思想,如果之前没有听说过的话,可以去爬虫的相关语法.如果是看网上的视频的话, ...
- 【Dotnet9-01】从0开始搭建开源项目-lqclass.com
行文目录 一. 前言 1.1 我的现有网站 1.2 想法:新开发一个网站 1.3 目前开发计划 二. 行动了 2.1 Github创建项目 2.2 使用 WTM 搭建后台框架 2.3 项目演示 2.4 ...
- 一文看懂 Kubernetes 服务发现: Service
Service 简介 K8s 中提供微服务的实体是 Pod,Pod 在创建时 docker engine 会为 pod 分配 ip,"外部"流量通过访问该 ip 获取微服务.但 ...
- ST 表练习笔记
P2048 [NOI2010]超级钢琴 首先按照 前缀和最大值 建立 \(ST\) 表 对于每一个 \(i\) 维护一个以他为起始点的最大的 "超级和弦" (\(ST\) 表 \( ...
- hashmap为什么是二倍扩容?
这个很简单,首先我们考虑一个问题,为什么hashmap的容量为2的幂次方,查看源码即可发现在计算存储位置时,计算式为: (n-1)&hash(key) 容量n为2的幂次方,n-1的二进制会全为 ...
- deepstrem编译缺少gst/gst.h解决方案
Deepstream在编译程序的程序的显示缺少gst/gst.h 具体情况是Deepstream运行已编译好的deepstream-app可以正常运行,但对源码编译的时候出现以述情况,初步分析是我们安 ...
- Java模拟表单POST上传文件
JAVA模拟表单POST上传文件 import java.awt.image.BufferedImage;import java.awt.image.ColorModel;import java.io ...
- 微信小程序图片上传和裁剪
本篇博客用于解决微信小程序图片裁剪问题 图片裁剪常用于头像选择和图片合成等. 图片裁剪解决方案: 目前网络上知名的微信小程序图片裁剪插件是we-cropper(文末有链接) 操作步骤:下载好we-cr ...