[LeetCode] 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 [3,2,1].

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

递归实现代码

/*****************************************************************
    *  @Author   : 楚兴
    *  @Date     : 2015/2/23 18:27
    *  @Status   : Accepted
    *  @Runtime  : 4 ms
******************************************************************/
struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL){}
};

class Solution {
public:
    vector<int> preorderTraversal(TreeNode *root) {
        vector<int> result;
        helper(root, result);

        return result;
    }

    void helper(TreeNode *root, vector<int>& num)
    {
        if (root)
        {
            num.push_back(root->val);
            helper(root->left, num);
            helper(root->right, num);
        }
    }
};

非递归实现代码1

/*****************************************************************
    *  @Author   : 楚兴
    *  @Date     : 2015/2/24 00:54
    *  @Status   : Accepted
    *  @Runtime  : 6 ms
******************************************************************/

class Solution {
public:
    vector<int> preorderTraversal(TreeNode *root) {
        vector<int> result;
        stack<TreeNode*> nodes;
        TreeNode *p = root;
        if (p)
        {
            nodes.push(p);
            while (!nodes.empty())
            {
                p = nodes.top();
                nodes.pop();
                result.push_back(p->val);
                if (p->right)
                {
                    nodes.push(p->right);
                }
                if (p->left)
                {
                    nodes.push(p->left);
                }
            }
        }

        return result;
    }
};

非递归实现代码2

/*****************************************************************
    *  @Author   : 楚兴
    *  @Date     : 2015/2/24 01:06
    *  @Status   : Accepted
    *  @Runtime  : 4 ms
******************************************************************/

class Solution {
public:
    vector<int> preorderTraversal(TreeNode *root) {
        vector<int> result;
        stack<TreeNode*> nodes;
        TreeNode *p = root;
        while (p != NULL || !nodes.empty())
        {
            if (p != NULL)
            {
                while(p != NULL)
                {
                    result.push_back(p->val);
                    nodes.push(p);
                    p = p->left;
                }
            }
            else
            {
                p = nodes.top()->right;
                nodes.pop();
            }
        }

        return result;
    }
};

后序遍历的实现见博文Binary Tree Postorder Traversal

时间: 2024-11-10 01:32:01

[LeetCode] Binary Tree Preorder Traversal的相关文章

[LeetCode]144.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? [代码一] /*******************************

[LeetCode 第8题] -- Binary Tree Preorder Traversal

题目链接: Binary Tree Preorder Traversal 题目意思: 给定一个二叉树根节点,求前序序列 代码: /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { publi

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]. 二叉树前序遍历的非递归版本,C++实现代码: #include<iostream> #include<new> #include<vector> #include<stack>

[LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)

目录:1.Binary Tree Level Order Traversal - 二叉树层次遍历 BFS 2.Binary Tree Level Order Traversal II - 二叉树层次遍历从低往高输出 BFS 3.Maximum Depth of Binary Tree - 求二叉树的深度 DFS4.Balanced Binary Tree - 判断平衡二叉树 DFS5.Path Sum - 二叉树路径求和判断DFS 题目概述:Given a binary tree, return

[LeetCode]94.Binary Tree Inorder Traversal

[题目] Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? confused what "{1,#,2,3}" means? &

[LeetCode]145.Binary Tree Postorder Traversal

[题目] Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? [代码] /*******************************

LeetCode: Binary Tree Level Order Traversal 层序遍历二叉树

 Binary Tree Level Order Traversal  题目描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 return its level ord

[LeetCode 第12题] -- Binary Tree Postorder Traversal

题目链接: Binary Tree PostOrder Trveral 题目意思: 给定一棵二叉树,求后续遍历序列 代码: /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public:

Binary Tree Inorder Traversal(转)

  Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3   return [1,3,2].   Java代码    /**   * Definition for a binary tree node.   * public class TreeNode {   *     int val;   *