码迷,mamicode.com
首页 > 其他好文 > 详细

代码题(6)— 二叉树的遍历

时间:2018-06-23 17:19:08      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:后序   turn   def   node   AC   solution   bin   二叉树的遍历   span   

1、144. 二叉树的前序遍历

(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> res;
    vector<int> preorderTraversal(TreeNode* root) {
        if(root == nullptr)
            return res;
        res.push_back(root->val);
        preorderTraversal(root->left);
        preorderTraversal(root->right);
        return res;
    }
};

 

(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> res;
        if(root == nullptr)
            return res;
        stack<TreeNode*> stNode;
        while(!stNode.empty() || root)
        {
            while(root)
            {
                stNode.push(root);
                res.push_back(root->val);
                root = root->left;
            }
            root = stNode.top();
            stNode.pop();
            root = root->right;
        }
        return res;
    }
};

 

 

2、94. 二叉树的中序遍历

 

(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> res;
    vector<int> inorderTraversal(TreeNode* root) {
        if(root != nullptr)
        {
            inorderTraversal(root->left);
            res.push_back(root->val);
            inorderTraversal(root->right);
        }
        return res;
    }
};

 

(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> inorderTraversal(TreeNode* root) {
        vector<int> res;
        if(root == nullptr)
            return res;
        stack<TreeNode*> stNode;
        while(!stNode.empty() || root)
        {
            while(root)
            {
                stNode.push(root);
                root = root->left;
            }
            root = stNode.top();
            stNode.pop();
            res.push_back(root->val);
            root = root->right;
        }
        return res;
    }
};

 

3、145. 二叉树的后序遍历

(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> res;
    vector<int> postorderTraversal(TreeNode* root) {
        if(root == nullptr)
            return res;
        postorderTraversal(root->left);
        postorderTraversal(root->right);  
        res.push_back(root->val);
        return res;
    }
};

 

(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> postorderTraversal(TreeNode* root) {
        vector<int> res;
        if(root == nullptr)
            return res;
        stack<TreeNode*> stNode;
        TreeNode* pre = nullptr;
        while(!stNode.empty() || root)
        {
            while(root)
            {
                stNode.push(root);
                root = root->left;
            }
            TreeNode* cur = stNode.top();
            if(cur->right == nullptr || pre == cur->right)  //如果没有右节点,或者右节点被访问过,则可以访问该节点
            {
                res.push_back(cur->val);
                stNode.pop();
                pre = cur;
            }
            else
                root = cur->right;
        }
        return res;
    }
};

 

代码题(6)— 二叉树的遍历

标签:后序   turn   def   node   AC   solution   bin   二叉树的遍历   span   

原文地址:https://www.cnblogs.com/eilearn/p/9217393.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!