牛客刷题二叉树之实现二叉树先序,中序和后序遍历

题目描述

分别按照二叉树先序,中序和后序打印所有的节点。
在这里插入图片描述
牛客链接:

https://www.nowcoder.com/practice/a9fec6c46a684ad5a3abd4e365a9d362?tpId=117&&tqId=35075&rp=1&ru=/ta/job-code-high&qru=/ta/job-code-high/question-ranking

解题思路:

用两种思路
递归和非递归

代码一:
递归

/**
 * struct TreeNode {
 *	int val;
 *	struct TreeNode *left;
 *	struct TreeNode *right;
 * };
 */

class Solution {
public:
    /**
     * 
     * @param root TreeNode类 the root of binary tree
     * @return int整型vector<vector<>>
     */
    vector<vector<int> > threeOrders(TreeNode* root) {
        // write code here
        vector<vector<int>> result;
        if(root == NULL) return result;
        vector<int> temp;
        preOrder(root, temp);
        result.push_back(temp);
        temp.clear();
        inOrder(root, temp);
        result.push_back(temp);
        temp.clear();
        postOrder(root, temp);
        result.push_back(temp);
        return result;
    }
    
    void preOrder(TreeNode* root, vector<int>& temp) {
        if(root == NULL) return;
        temp.push_back(root->val);
        if(root->left != NULL) {
            preOrder(root->left, temp);
        }
        if(root->right != NULL) {
            preOrder(root->right, temp);
        }
    }
    
    void inOrder(TreeNode* root, vector<int>& temp) {
        if(root == NULL) return;
        if(root->left != NULL) {
            inOrder(root->left, temp);
        }
        temp.push_back(root->val);
        if(root->right != NULL) {
            inOrder(root->right, temp);
        }
    }
    
    void postOrder(TreeNode* root, vector<int>& temp) {
        if(root == NULL) return;
        if(root->left != NULL) {
            postOrder(root->left, temp);
        }
        if(root->right != NULL) {
            postOrder(root->right, temp);
        }
        temp.push_back(root->val);
    }
};

代码二:
非递归

/**
 * struct TreeNode {
 *	int val;
 *	struct TreeNode *left;
 *	struct TreeNode *right;
 * };
 */

class Solution {
public:
    /**
     * 
     * @param root TreeNode类 the root of binary tree
     * @return int整型vector<vector<>>
     */
    vector<vector<int> > threeOrders(TreeNode* root) {
        // write code here
        vector<vector<int>> result;
        if(root == NULL) return result;
        vector<int> temp;
        preOrder(root, temp);
        result.push_back(temp);
        temp.clear();
        inOrder(root, temp);
        result.push_back(temp);
        temp.clear();
        postOrder(root, temp);
        result.push_back(temp);
        return result;
    }
    
    void preOrder(TreeNode* root, vector<int>& temp) {
        stack<TreeNode*> stk;
        TreeNode* tempNode = root;
        while(!stk.empty() || tempNode!=NULL) {
            if(tempNode != NULL) {
                temp.push_back(tempNode->val);
                stk.push(tempNode);
                tempNode = tempNode->left;
            } else {
                tempNode = stk.top();
                stk.pop();
                tempNode = tempNode->right;
            }
        }
        return ;
    }
    
    void inOrder(TreeNode* root, vector<int>& temp) {
        stack<TreeNode*> stk;
        TreeNode* tempNode = root;
        while(!stk.empty() || tempNode!=NULL) {
            if (tempNode != NULL) {
                stk.push(tempNode);
                tempNode = tempNode->left;
            } else {
                tempNode = stk.top();
                stk.pop();
                temp.push_back(tempNode->val);
                tempNode = tempNode->right;
            }
        }
        return ;
    }
    
    void postOrder(TreeNode* root, vector<int>& temp) {
        stack<TreeNode*> stk;
        TreeNode* tempNode = root;
        TreeNode* pre = NULL;
        while(!stk.empty() || tempNode!=NULL) {
            if (tempNode != NULL) {
                stk.push(tempNode);
                tempNode = tempNode->left;
            } else {
                if (stk.top()->right==NULL || stk.top()->right==pre) {
                    pre = stk.top();
                    stk.pop();
                    temp.push_back(pre->val);
                } else {
                    tempNode = stk.top()->right;
                }
            }
        }
        return ;
    }
};

注意:
非递归中栈的使用
后序遍历的非递归方式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值