midtorder/preorder/postorder

题目一:midorder

vector<int> midorderTraversal(TreeNode *root) {
     vector<int> res;
     if(root==NULL) return res;
     TreeNode *p=root;
     stack<TreeNode*> stk;
     while(p || !stk.empty())
     {
         if(p)
         {
             stk.push(p);
             p=p->left;
         }
         else
         {
             TreeNode *q=stk.top();
             res.push_back(q->val);
             stk.pop();
             if(q->right)
             {
                 p=q->right;
                 stk.push(p);//这两句可以没有
                 p=p->left;//这两句可以没有
             }
             else
                 p=NULL;
         }
     }
     return res;

注:斜线注释的那两行可以不要;第18-25行可以用一句话代替:p=q->right;

题目二:preorder

/**
 * Definition for binary tree
 * 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;
        stack<TreeNode *> stk;
        if(root==NULL)
            return res;
        while(root || !stk.empty())
        {
            if(root)
            {
                res.push_back(root->val);
                stk.push(root);
                root=root->left;
            }
            else{
                TreeNode *p=stk.top();
                stk.pop();
                if(p->right)
                {
                    root=p->right;
                }
                else
                    root=NULL;//这句话可以没有
            }
        }
        return res;
    }
};

题目三:postorder

/**
 * Definition for binary tree
 * 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;
        stack<TreeNode *> stk;
        if(root==NULL)
            return res;
        TreeNode *r=NULL;
        while(root || !stk.empty())
        {
            if(root)
            {
                stk.push(root);
                root=root->left;
            }
            else
            {
                TreeNode *p=stk.top();
                if(p->right && p->right!=r)
                {
                    root=p->right;
                }
                else
                {
                    res.push_back(p->val);
                    stk.pop();
                    r=p;
                    root=NULL;//这句可以没有
                }
            }
        }
        return res;
    }
};
改进下面的这段代码使它能正常运行:// Tree traversal in C #include <stdio.h> #include <stdlib.h> struct node { int item; struct node* left; struct node* right; }; // Inorder traversal void inorderTraversal(struct node* root) { if(root) { inorderTraversal(root->left); printf("%d",root->item); inorderTraversal(root->right); } } // Preorder traversal void preorderTraversal(struct node* root) { printf("%d",root->item); preorderTraversal(root->left); preorderTraversal(root->right); } // Postorder traversal void postorderTraversal(struct node* root) { postorderTraversal(root->left); postorderTraversal(root->right); printf("%d",root->item); } // Create a new Node struct node* create(int value) { struct node* newNode = malloc(sizeof(struct node)); newNode->item = value; newNode->left = NULL; newNode->right = NULL; return newNode; } // Insert on the left of the node struct node* insertLeft(struct node* root, int value) { root->left=create(value); return root->left; } // Insert on the right of the node struct node* insertRight(struct node* root, int value) { root->right=create(value); return root->right; } int main() { struct node* root = create(1); insertLeft(root, 4); insertRight(root, 6); insertLeft(root->left, 42); insertRight(root->left, 3); insertLeft(root->right, 2); insertRight(root->right, 33); printf("Traversal of the inserted binary tree \n"); printf("Inorder traversal \n"); inorderTraversal(root); printf("\nPreorder traversal \n"); preorderTraversal(root); printf("\nPostorder traversal \n"); postorderTraversal(root); }
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值