栈和递归的关系 144:Binary Tree Preorder Traversal

前序遍历:根左右

//用栈来实现非递归解法/**
 * 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> res;
        if(root == NULL)
            return res;
        stack<TreeNode*> stack;
        stack.push(root);

        while(!stack.empty()){
            TreeNode* c = stack.top();
            stack.pop();
            res.push_back(c->val);
            if(c->right)
                stack.push(c->right);
            if(c->left)
                stack.push(c->left);
        }
        return res;
    }
};
//递归解法,注意res是全局的,要放在遍历函数外面/**
 * 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> res;
    vector<int> preorderTraversal(TreeNode* root) {

        if(root){
            res.push_back(root->val);
            preorderTraversal(root->left);
            preorderTraversal(root->right);
        }
        return res;
    }
};

中序遍历:左根右

/**
 * 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> inorderTraversal(TreeNode* root) {
        stack<TreeNode*> st;
        vector<int> res;
        if(root == NULL) return res;  //若根节点为空则返回空
        TreeNode* p = root;
        while(p || !st.empty()){
            while(p){
                //先将根节点入栈,再将它所有的左节点入栈
                st.push(p);
                p = p->left;
            }

            p = st.top();
            st.pop();
            res.push_back(p->val);
            p = p->right;
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/Bella2017/p/10279952.html

时间: 2024-10-14 17:13:16

栈和递归的关系 144:Binary Tree Preorder Traversal的相关文章

LeetCode 144. Binary Tree Preorder Traversal 解题报告

144. Binary Tree Preorder Traversal My Submissions Question Total Accepted: 108336 Total Submissions: 278322 Difficulty: Medium Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3

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? 题目链接:https://leetcode.com/problems/binary-tre

144.Binary Tree Preorder Traversal(非递归前序遍历)

Given a binary tree, return the preorder traversal of itsnodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,2,3]. Note: Recursive solution istrivial, could you do it iteratively? HideTags Tree Stack #pragma once #include<ios

Java [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]. 解题思路: 这个问题最简单的方法是使用递归,但是题目规定不能使用,得使用迭代的方法. 那么我们考虑使用栈来实现. 思路是每次遍历这节点,把该点的值放入list中,然后把该点的右孩子放入栈中,并将当前点设置为左孩子

144 Binary Tree Preorder Traversal(二叉树先序遍历Medium)

题目意思:二叉树先序遍历,结果存在vector<int>中 解题思路:1.递归(题目中说用递归做没什么意义,我也就贴贴代码吧) 2.迭代 迭代实现: 1 class Solution { 2 public: 3 vector<int> preorderTraversal(TreeNode* root) { 4 vector<int> ans; 5 if(root){ 6 TreeNode* temp; 7 stack<TreeNode*> s; //利用栈,

【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 144. Binary Tree Preorder Traversal

前序遍历 递归: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<int> res;

[leedcode 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]. /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right;

Java for 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]. 二叉树的前序遍历,根节点→左子树→右子树 解题思路一: 递归实现,JAVA实现如下: public List<Integer> preorderTraversal(TreeNode root) { List<Int