Skip to content

Latest commit

 

History

History
89 lines (65 loc) · 1.84 KB

File metadata and controls

89 lines (65 loc) · 1.84 KB

Binary Tree Preorder Traversal

描述

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

For example: Given binary tree {1,#,2,3},

 1
  \
   2
  /
 3

return [1,2,3].

Note: Recursive solution is trivial, could you do it iteratively?

分析

用栈或者Morris遍历。

{% if book.java %}

// Binary Tree Preorder Traversal
// 使用栈,时间复杂度O(n),空间复杂度O(n)
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        ArrayList<Integer> result = new ArrayList<>();
        Stack<TreeNode> s = new Stack<>();
        if (root != null) s.push(root);

        while (!s.isEmpty()) {
            final TreeNode p = s.pop();
            result.add(p.val);

            if (p.right != null) s.push(p.right);
            if (p.left != null) s.push(p.left);
        }
        return result;
    }
}

{% endif %}

{% if book.cpp %}

// Binary Tree Preorder Traversal
// 使用栈,时间复杂度O(n),空间复杂度O(n)
class Solution {
public:
    vector<int> preorderTraversal(TreeNode *root) {
        vector<int> result;
        stack<const TreeNode *> s;
        if (root != nullptr) s.push(root);

        while (!s.empty()) {
            const TreeNode *p = s.top();
            s.pop();
            result.push_back(p->val);

            if (p->right != nullptr) s.push(p->right);
            if (p->left != nullptr) s.push(p->left);
        }
        return result;
    }
};

{% endif %}

Morris先序遍历

{% codesnippet "./code/binary-tree-preorder-traversal-2."+book.suffix, language=book.suffix %}{% endcodesnippet %}

相关题目