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

Posted stAr_1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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二叉树前序遍历的主要内容,如果未能解决你的问题,请参考以下文章

#Leetcode# 144. Binary Tree Preorder Traversal

LeetCode 144. Binary Tree Preorder Traversal

[leetcode-144-Binary Tree Preorder Traversal]

LeetCode-144-Binary Tree Preorder Traversal

leetcode [144]Binary Tree Preorder Traversal

Java [Leetcode 144]Binary Tree Preorder Traversal