树的先序遍历、中序遍历、后续遍历的递归与循环详解

针对二叉树

满二叉树:特点是树很丰满,深度为h且有2^h-1个节点的二叉树,每个叶子节点的深度都相同

完全二叉树:如果一个二叉树除了最右边位置有叶子节点的缺少,其他很丰满。

编号:如果父节点为K,左子为2K 右子为2K+1

先序遍历,中序遍历,后续遍历,先中后指的是根节点在遍历时候的位置。

中序(Inorder)->递归

/**
 * 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:
    void inorderHelper(TreeNode* root, vector<int>& res){
        if(root == NULL) return;
        if(root->left){
            inorderHelper(root->left,res);
        }
        res.push_back(root->val); // 代表输出,即先遍历根节点
        if(root->right){
            inorderHelper(root->right,res);
        }
    }
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int>res;
        if(root == NULL) return res;
        inorderHelper(root, res);
        return res;


    }
};

中序(Inorder)->->循环
用栈,对左侧做压栈,到最左子节点开始遍历,然后check最左子节点有没有右子,如果有继续压栈遍历,如果没有就开始从栈顶取数据。

/**
 * 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 == NULL) return res;
        stack<TreeNode*> Sroot;
        TreeNode* temp = root;
        while(Sroot.size() != 0 || temp){
            if(temp){
                Sroot.push(temp);
                temp = temp->left;

            }
            else{
                res.push_back(Sroot.top()->val);
                temp = Sroot.top();
                Sroot.pop();
                temp = temp->right;
            }


        }
        return res;


    }
};

先序->循环
先压右,后压左

vector<int> preOrder(TreeNode *root){
    vector<int> res;
    if(root == NULL) return res;
    TreeNode* temp = root;
    stack<TreeNode*> Sroot;
    Sroot.push(temp);

    while(Sroot.size() !=0){
         res.push_back(Sroot.top()->val);
         temp = Sroot.top();
         Sroot.pop();
         if(temp->right) 
            Sroot.push(temp->right);
         if(temp->left)
            Sroot.push(temp->left);



    }
    return res;

后序遍历,看了leetcode上长姿势的解法,可能是我太蠢

My Accepted code with explaination. Does anyone have a better idea? pre-order traversal is root-left-right, and post order is left-right-root. modify the code for pre-order to make it root-right-left, and then reverse the output so that we can get left-right-root .

Create an empty stack, Push root node to the stack. Do following while stack is not empty.
2.1. pop an item from the stack and print it.

2.2. push the left child of popped item to stack.

2.3. push the right child of popped item to stack.

reverse the ouput.

class Solution {
public:
    vector<int> postorderTraversal(TreeNode *root) {
        stack<TreeNode*> nodeStack;
        vector<int> result;
        //base case
        if(root==NULL)
        return result;
        nodeStack.push(root);
    while(!nodeStack.empty())
    {
        TreeNode* node= nodeStack.top();  
        result.push_back(node->val);
        nodeStack.pop();
        if(node->left)
        nodeStack.push(node->left);
        if(node->right)
        nodeStack.push(node->right);
    }
     reverse(result.begin(),result.end());
     return result;

}

};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章