144. 二叉树的前序遍历

144. Binary Tree Preorder Traversal

题目描述

给定一个二叉树,返回它的 前序 遍历。

LeetCode144. Binary Tree Preorder Traversal

示例:

输入: [1,null,2,3]

  1. 1
  2. \
  3. 2
  4. /
  5. 3

输出: [1,2,3]

进阶: 递归算法很简单,你可以通过迭代算法完成吗?

Java 实现

Iterative Solution

  1. import java.util.LinkedList;
  2. import java.util.List;
  3. import java.util.Stack;
  4. class TreeNode {
  5. int val;
  6. TreeNode left;
  7. TreeNode right;
  8. TreeNode(int x) {
  9. val = x;
  10. }
  11. }
  12. class Solution {
  13. public List<Integer> preorderTraversal(TreeNode root) {
  14. List<Integer> result = new LinkedList<>();
  15. if (root == null) {
  16. return result;
  17. }
  18. Stack<TreeNode> stack = new Stack<>();
  19. stack.push(root);
  20. while (!stack.isEmpty()) {
  21. TreeNode node = stack.pop();
  22. if (node.right != null) {
  23. stack.push(node.right);
  24. }
  25. if (node.left != null) {
  26. stack.push(node.left);
  27. }
  28. result.add(node.val);
  29. }
  30. return result;
  31. }
  32. }

Recursive Solution

  1. import java.util.LinkedList;
  2. import java.util.List;
  3. class TreeNode {
  4. int val;
  5. TreeNode left;
  6. TreeNode right;
  7. TreeNode(int x) {
  8. val = x;
  9. }
  10. }
  11. class Solution {
  12. List<Integer> result = new LinkedList<>();
  13. public List<Integer> preorderTraversal(TreeNode root) {
  14. if (root == null) {
  15. return result;
  16. }
  17. result.add(root.val);
  18. preorderTraversal(root.left);
  19. preorderTraversal(root.right);
  20. return result;
  21. }
  22. }

相似题目

参考资料

LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)的更多相关文章

  1. [Swift]LeetCode144. 二叉树的前序遍历 | Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...

  2. [Leetcode 144]二叉树前序遍历Binary Tree Preorder Traversal

    [题目] Given a binary tree, return the preordertraversal of its nodes' values. Example: Input: [1,null ...

  3. Java实现 LeetCode 144 二叉树的前序遍历

    144. 二叉树的前序遍历 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] /** * Definition for a ...

  4. LeetCode 589. N叉树的前序遍历(N-ary Tree Preorder Traversal)

    589. N叉树的前序遍历 589. N-ary Tree Preorder Traversal LeetCode589. N-ary Tree Preorder Traversal 题目描述 给定一 ...

  5. LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)

    题目描述 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 由 ...

  6. Leetcode 144.二叉树的前序遍历

    1.题目描述 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 2.解法 ...

  7. LeetCode 102. 二叉树的层次遍历(Binary Tree Level Order Traversal) 8

    102. 二叉树的层次遍历 102. Binary Tree Level Order Traversal 题目描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 每 ...

  8. LeetCode 144. 二叉树的前序遍历 (非递归)

    题目链接:https://leetcode-cn.com/problems/binary-tree-preorder-traversal/ 给定一个二叉树,返回它的 前序 遍历. /** * Defi ...

  9. LeetCode 144 ——二叉树的前序遍历

    1. 题目 2. 解答 2.1. 递归法 定义一个存放树中数据的向量 data,从根节点开始,如果节点不为空,那么 将当前节点的数值加入到 data 中 递归得到其左子树的数据向量 temp,将 te ...

随机推荐

  1. MATLAB曲线拟合函数

    一.多项式拟合 ployfit(x,y,n) :找到次数为 n 的多项式系数,对于数据集合 {(x_i,y_i)},满足差的平方和最小 [P,E] = ployfit(x,y,n) :返回同上的多项式 ...

  2. 《自制编程语言--基于C语言 郑钢》学习笔记

    <自制编程语言>学习笔记 本仓库内容 <自制编程语言>源码 src/sparrow.tgz <自制编程语言>读书笔记 docs/* <自制编程语言>样章 ...

  3. 常用spaceclaim脚本

    #创建一个长方体,通过两点来确定一个立方体 #MM表示的是以毫米作为单位 #返回的是一个BlockBody的对象 #本函数还有第三个参数可选,分别代表增加材料,切除材料等等 #默认值为增加材料 注:第 ...

  4. SignalR了解

    一.概述 一.理解SignalR ASP.NET SignalR 是为 ASP.NET 开发人员提供的一个库,可以简化开发人员将实时 Web 功能添加到应用程序的过程. 实时 Web 功能是指这样一种 ...

  5. Kafka(一) —— 基本概念及使用

    一.安装&启动 安装Kafka(使用内置Zookeeper) 在Kafka官网下载安装包kafka_2.11-1.0.0.tgz #### 解压 tar zxvf kafka_2.11-1.0 ...

  6. PKUWC2020 游记

    因为CSP-S挂的并不厉害,蜜汁来到了PKU,所以有了这篇游记. DAY 0 上午在机房颓废,中途还整了一个出校证. 九点多,两个THU的大神去拿笔记本和手机颓废了,不久被两个教练拉着和kx跑了出去. ...

  7. 【转】python requests库添加自定义cookie的方法

    requests库是个很方便的爬虫,相关文档已经很详细了.不过我今天在爬网页时,有一个网站是在脚本中添加cookie的,但我向requests.cookies里添加cookie费了不少周折.尝试了多个 ...

  8. idea断点调试学习随笔

    1,rerun XXX,这个就是直接重新跑某个程序.2,这个相当于eclipse里面的f8,直接跑完,到下一个断点停下,没有就直接跑完程序.3,停止项目或者程序.要是自己的main呢,点一下就停下了, ...

  9. [root@offical nginx]# nginx -t nginx: [emerg] module "/usr/lib64/nginx/modules/ngx_http_geoip_module.so" version 1012002 instead of 1016001 in /usr/share/nginx/modules/mod-http-geoip.conf:1 nginx: con

    [root@offical nginx]# nginx -tnginx: [emerg] module "/usr/lib64/nginx/modules/ngx_http_geoip_mo ...

  10. nodejs设置淘宝镜像

    nodeJS的资源仓库在国内使用过程中,偶尔会遇到各种资源问题,通常设置为淘宝的镜像,网上很多说法是安装淘宝镜像,即$ npm install -g cnpm --registry=https://r ...