leetcode:144. Binary Tree Preorder Traversal

原创 2016年03月19日 22:09:45

转载请注明出处:z_zhaojun的博客
原文地址
题目地址
Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values.

For example:
Given binary tree {1,#,2,3},
return [1,2,3].

思路:优先添加遍历左子树,在遍历的同时判断当前节点的右子树是否为空,如果不为空则添加到nodeList(类似栈,实现先进后出,最晚add的最先remove)中,当左子树遍历完毕后,再通过nodeList进行右子树的遍历。
实现代码(Java):

public class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> valList = new ArrayList<>();
        List<TreeNode> nodeList = new ArrayList<>();
        while (root != null) {
            valList.add(root.val);
            if (root.right != null) {
                nodeList.add(root.right);
            }
            if (root.left != null) {
                root = root.left;
            } else {
                if (nodeList.size() > 0) {
                    root = nodeList.get(nodeList.size() - 1);
                    nodeList.remove(nodeList.size() - 1);
                } else {
                    break;
                }
            }
        }
        return valList;
    }
}
版权声明:本文为博主原创文章,转载请注明出处。博客首页:http://blog.csdn.net/u012975705。订阅:http://blog.csdn.net/u012975705/rss/list https://blog.csdn.net/u012975705/article/details/50933466

LeetCode 144 — Binary Tree Preorder Traversal(C++ Java Python)

题目:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ Given a binary tree, return the p...
  • dragon_dream
  • dragon_dream
  • 2014-03-02 22:13:14
  • 2589

LeetCode(144)Binary Tree Preorder Traversal

题目如下: Given a binary tree, return the preorder traversal of its nodes' values. For example: Given bi...
  • feliciafay
  • feliciafay
  • 2014-01-15 06:29:16
  • 1766

【LeetCode】144 . Binary Tree Preorder Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/pro...
  • crazy1235
  • crazy1235
  • 2016-05-22 23:53:41
  • 5599

LeetCode144 Binary Tree Preorder Traversal(迭代实现) Java

题目: Given a binary tree, return the preorder traversal of its nodes’ values.For example: Given bin...
  • l294265421
  • l294265421
  • 2016-04-23 13:14:19
  • 1602

LeetCode 144 Binary Tree Preorder Traversal(二叉树的前序遍历)+(二叉树、迭代)

翻译给定一个二叉树,返回其前序遍历的节点的值。例如: 给定二叉树为 {1,#, 2, 3} 1 \ 2 / 3 返回 [1, 2, 3]备注:用递归是微不足道的,...
  • NoMasp
  • NoMasp
  • 2016-03-19 16:15:45
  • 3364

LeetCode 144. Binary Tree Preorder Traversal (前序遍历的非递归实现)

前序遍历。用栈node_stack维护要访问的结点指针。 每次访问栈顶,输出结点的值...
  • u014674776
  • u014674776
  • 2014-06-02 02:35:40
  • 491

leetcode-java-144. Binary Tree Preorder Traversal

使用循环来解决,递归和迭代都是循环 递归:递归重复调用自身实现循环 迭代:循环代码中参与运算的变量同时是保存结果的变量,当前保存的结果作为下一次循环计算的初始值 普通循环: 思路一: 使用递...
  • github_34514750
  • github_34514750
  • 2016-06-29 16:46:14
  • 200

【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/pro...
  • crazy1235
  • crazy1235
  • 2016-06-01 23:42:08
  • 6342

LeetCode笔记:144. Binary Tree Preorder Traversal

用循环操作来前序遍历二叉树
  • Cloudox_
  • Cloudox_
  • 2017-03-23 09:33:24
  • 506

LeetCode 105:Construct Binary Tree from Preorder and Inorder Traversal

LeetCode 105: Given preorder and inorder traversal of a tree, construct the binary tree. 给定一个二叉树的...
  • sunao2002002
  • sunao2002002
  • 2015-05-20 23:09:41
  • 1128
收藏助手
不良信息举报
您举报文章:leetcode:144. Binary Tree Preorder Traversal
举报原因:
原因补充:

(最多只允许输入30个字)