[LeetCode] Binary Tree Preorder Traversal (非递归的先序遍历)

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

解题思路:

二叉树的前序遍历。题目要求用非递归的方法解答。那我们先看一下递归方法的解法。

1、递归解法。

/**
 * Definition for a binary tree node.
 * 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;
        preorderHelper(result, root);
        return result;
    }

    void preorderHelper(vector<int>& result, TreeNode* root){
        if(root==NULL){
            return;
        }
        result.push_back(root->val);
        preorderHelper(result, root->left);
        preorderHelper(result, root->right);
    }
};

2、非递归解法

我们可以用两个数据结构来存储中间状态。用队列来存储左孩子,用栈来存储右孩子。优先遍历所有左孩子。

/**
 * Definition for a binary tree node.
 * 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;
        queue<TreeNode*> l;
        stack<TreeNode*> r;
        if(root!=NULL){
            l.push(root);
        }
        while(!l.empty()||!r.empty()){
            TreeNode* node = NULL;
            if(!l.empty()){
                node=l.front();
                l.pop();
            }else{
                node=r.top();
                r.pop();
            }
            result.push_back(node->val);
            if(node->left!=NULL){
                l.push(node->left);
            }
            if(node->right!=NULL){
                r.push(node->right);
            }
        }

        return result;
    }
};
时间: 2024-10-10 18:01:35

[LeetCode] Binary Tree Preorder Traversal (非递归的先序遍历)的相关文章

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}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? //采用二叉树前序遍历的方法来做******************************

leetcode - Binary Tree Preorder Traversal &amp;&amp; Binary Tree Inorder Traversal &amp;&amp; Binary Tree Postorder Traversal

简单来说,就是二叉树的前序.中序.后序遍历,包括了递归和非递归的方法 前序遍历(注释中的为递归版本): 1 #include <vector> 2 #include <stack> 3 #include <stddef.h> 4 #include <iostream> 5 6 using namespace std; 7 8 struct TreeNode 9 { 10 int val; 11 TreeNode *left; 12 TreeNode *rig

LeetCode: Binary Tree Preorder Traversal [144]

[题目] 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]Binary Tree Preorder Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ 题意:这题用递归比较简单.应该考察的是使用非递归实现二叉树的先序遍历.先序遍历的遍历顺序是:根,左子树,右子树. 解题思路:如果树为下图: 1 /     \ 2         3 /     \    /    \ 4       5  6     7 使用一个栈.步骤为: 一,先遍历节点1,并入栈,如果有左孩子,继续遍历并入栈,一直到栈为{1,2,4}.

leetcode 题解: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? 说明:1)下面有两种实现:递归(Recursive )与非递归(迭代iterative

LeetCode: Binary Tree Preorder Traversal 解题报告

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    /   3return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively?

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}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 二叉树的前序遍历 先看递归的写法(C++): /** * Definition f

【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}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 思路: 写个非递归的前序遍历,用 stack. 代码: C++: /** * De

LeetCode——Binary Tree Preorder Traversal

Description: 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]. 经典的二叉树前序遍历,用递归闭着眼写. /** * Definition for a binary tree node. * public class TreeNode { * int val; *