C++刷题模板之二叉树遍历篇

C++刷题笔记

1、二叉树 Binary Tree

- Linked List 是特殊化的Tree
- Tree是特殊化的Graph
  • 二叉树的遍历一般Pre-order, In-order, Post-order
    • 前序遍历pre-order: 根-左-右
    • 中序遍历in-order: 左=根-右
    • 后续遍历post-order: 左-右-根

2、二叉搜索树 Binary Search Tree

- 二叉搜索树,也叫二叉排序树,有序二叉树(order Binary Tree)
- 排序二叉树(Sorted Binary Tree)是指一棵宝树或者具有下列性质的二叉树:
    - 1. 左子树上的所有结点值均小于它的根节点的值。
    - 2. 右子树上的所有结点值均大于它的根节点的值。
    - 3. 以此类推,左右子树也分别为二叉查找树(这就是重复性)
  • 二叉搜索树的常见操作:
    • 1、查询 O(logn)
    • 2、插入新结点(创建) O(logN)
    • 3、删除
二叉树的遍历
二叉树的结构体
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */

1、94-binary-tree-inorder-traversal 二叉树的中序遍历 - Medium

  • 给定一个二叉树的根节点root,返回它的中序遍历
  • 1、使用递归的
class Solution {
public:
//2.times 使用递归的方式,
    void InorderTree(TreeNode* root, vector<int>& ans) {
        if(!root)   
         return ; 
        if(root ->left)
            InorderT(root ->left, ans);
        ans.push_back(root ->val);
        if(root ->right)
            InorderT(root ->right, ans); 
    }
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int>ans; 
        if(!root)
            return ans; 
        InorderTree(root, ans); 
        return ans; 
    }
};
  • 2、利用栈进行模拟
class Solution{
public:
    vector<int> inOrderTrtaversal(TreeNode* root) {
        vector<int>ans; 
        stack<TreeNode*>tree_stack; 
        while(root || tree_stack.size()){
            while(root) {
                tree_stack.push(root); 
                root = root ->left;
            }
            root = tree_stack.top(); 
            tree_stack.pop(); 
            ans.push_back(root -> val); 
            root = root =>right; 
        }
        return ans; 
    }
}

2、 144-binary-tree-preorder-traversal - Medium

  • 给你二叉树的根节点 root ,返回它节点值的前序遍历。
  • 1、利用栈模拟的方式:
class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int>ans; 
        if(!root)
            return ans; 
        stack<TreeNode*>s_tree; 
        s_tree.push(root);
        while( !s_tree.empty()) {
            TreeNode* cur = s_tree.top(); 
            s_tree.pop(); 
            ans.push_back(cur ->val); 
            if(cur ->left)
                s_tree.push(cur ->left);
            if(cur ->right)
                s_tree.push(cur ->right);
        }
        reverse(ans.begin(), ans.end());
        return ans; 
    }
};
  • 2、使用递归的方式:
class Solution {
public:
    //使用递归的方法进行
    void Traversal(TreeNode* root, vector<int>& ans) { // 这里对对于vector的传入方式应该是选择以引用的方式进行参数传递
        if(!root)
            return; 
        ans.push_back(root ->val); 
        Traversal(root ->left, ans);
        Traversal(root ->right, ans);
    }
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int>ans; 
        Traversal(root, ans);
        return ans; 
    }
};

3、 145-binary tree postorder traversal - Mediun

  • 给你一个二叉树,返回它的后序遍历
  • 1、使用栈进行模拟
class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int>ans; 
        if(!root)
            return ans; 
        stack<TreeNode*>s_tree; 
        s_tree.push(root);
        while( !s_tree.empty()) {
            TreeNode* cur = s_tree.top(); 
            s_tree.pop(); 
            ans.push_back(cur ->val); 
            if(cur ->left)
                s_tree.push(cur ->left);
            if(cur ->right)
                s_tree.push(cur ->right);
        }
        reverse(ans.begin(), ans.end());
        return ans;   
    }
};
  • 2、使用递归的方式
class Solution{
public:
    void PostTraversal(TreeNode* root, vector<int>& ans) {
        if(! root)
            return ; 
        if(root -> left)
            PostTraversal(root ->left, ans); 
        if(root ->right)
            PostTraversal(root ->right, ans); 
        ans.push_back(root -> val); 
    }
    vector<int> postOrderTraversal(TreeNode* root) {
        vector<int>ans; 
        if(! root)
            return ans; 
        postTraversal(root, ans); 
        return ans;
    }
}
n叉树的遍历
n叉树的结构体
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;
    Node() {}
    Node(int _val) {
        val = _val;
    }
    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};

4、590- n-ary-tree-postorder-traversal - Easy

  • 给定一个n叉树,返回其节点值的后序遍历
  • 1、利用栈模拟
class Solution {
public:
    vector<int> postorder(Node* root) {
        vector<int>ans; 
        if(!root) 
            return ans; 
        stack<Node*>s_tree; 
        s_tree.push(root);
        while(! s_tree.empty()) {
            Node* top = s_tree.top(); 
            s_tree.pop();
            ans.push_back(top ->val); 
            for(int i = 0; i< top ->children.size(); ++i ) {
                if(top ->children[i])
                    s_tree.push(top ->children[i]);
            }
        }
        reverse(ans.begin(), ans.end()); 
        return ans; 
    }
};
  • 2、使用递归方法
class Solution {
public:
    void Traversal(Node* root, vector<int>&ans) {
        if(! root)
            return ; 
        for(int i = 0; i< root->children.size(); ++i) {
            Traversal(root ->children[i], ans);
        }
        ans.push_back(root ->val);
    }
    vector<int> postorder(Node* root) {
        vector<int>ans; 
        if(! root)
            return ans; 
        Traversal(root, ans);
        return ans; 
        
    }
};

5、589- N叉树的前序遍历- Easy

  • 给定一个n叉树,返回其节点值的前序遍历
    1. 使用递归的方式:
class Solution {
public:
    // 递归方式
    void pre(Node* root, vector<int>& ans) {
        if(!root)
            return ; 
        ans.emplace_back(root ->val); 
        for(int i = 0; i< root ->children.size(); ++ i)
            pre(root ->children[i], ans); 
    }
    vector<int> preorder(Node* root) {
        vector<int>ans; 
        if(!root)
            return ans; 
        pre(root, ans); 
        return ans;
    }
};
    1. 使用迭代,栈模拟的方式
class Solution {
public:
    vector<int> preorder(Node* root) {
        vector<int>ans; 
        if(!root) 
            return ans; 
        stack<Node*>s_tree; 
        s_tree.push(root);
        while(! s_tree.empty()) {
            Node* top = s_tree.top(); 
            s_tree.pop();
            ans.push_back(top ->val); 
            for(int i = top ->children.size()-1;i >=0; --i ) {
                if(top ->children[i])
                    s_tree.push(top ->children[i]);
            }
        }
        //reverse(ans.begin(), ans.end()); 
        return ans; 
    }
};

6、 429- N叉树的层序遍历 - Medium

  • 给定一个N叉树,返回其节点值的层序遍历,(即从左到右,逐层遍历)
    1. dfs的方式:
class Solution {
public:
    void dfs(Node* root, vector<vector<int>>& ans, int levels) {
        if(!root)
            return ; 
        if(ans.size() == levels)
            ans.push_back(vector<int>());
        ans[levels].push_back(root->val); 
        for(auto &c: root ->children)
            dfs(c, ans, levels+1); 
    }
    vector<vector<int>> levelOrder(Node* root) {
        vector<vector<int>>ans; 
        if(! root)
            return ans; 
        dfs(root, ans, 0); 
        return ans; 
    }
};
    1. 迭代,栈模拟方式:
class Solution {
public:
    vector<vector<int>> levelOrder(Node* root) {
        vector<vector<int>> ans; 
        if(! root)
            return ans; 
        queue<Node*>q_tree; 
        q_tree.push(root);
        while(!q_tree.empty()) {
            vector<int> cur_ver; 
            int size_q = q_tree.size(); 
            for(int i = 0; i< size_q; ++ i) {
                Node* cur = q_tree.front(); 
                q_tree.pop(); 
                cur_ver.push_back(cur ->val); 
                for(int j = 0; j < cur ->children.size(); ++j ) {
                    if(cur ->children[j])
                        q_tree.push(cur ->children[j]);
                }
            }
            ans.push_back(cur_ver);
        }
        return  ans; 
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值