返回顶部

A 第五课 二叉树与图

内容概述及预备知识

 

预备知识:二叉树定义

代码中的构造 二叉树 :

#include <iostream>
using namespace std;

/*
 * 二叉树的构造!
 * */

class TreeNode{ // class 默认是private visiblity | 而c++ 中struct 默认是public visiblity!

public:
    int val;
    TreeNode* left;
    TreeNode* right;

    TreeNode(int val){
        this->val = val;
        left = NULL;
        right = NULL;
    }
};

// 前序(先根再左后右)遍历二叉树
/*
 * TreeNode * node: 正在遍历的节点 
 * int layer: 当前节点的层数 
 * */
void preorder_print(TreeNode * node,int layer){
    if (node == NULL){
        return;
    }
    for (int i = 0;i < layer; ++ i) {
        cout << "----";// 根据层数 打印 ----  
    }
    cout << "["<< node->val<< "]"<<endl;

    preorder_print(node->left,layer+1);
    preorder_print(node->right,layer+1);
}

void inorder_print(TreeNode* node,int layer){
    if (node == NULL)
        return;

    inorder_print(node->left,layer+1);
    for (int i = 0; i < layer; ++i) {
        cout << "----";// 根据层数 打印 ----  
    }
    cout <<"[" << node->val << "]"<< endl;
    inorder_print(node->right,layer+1);
}
void postorder_print(TreeNode* node,int layer){
    if(node == NULL)
        return ;

    postorder_print(node->left,layer+1);
    postorder_print(node->right,layer+1);
    for (int i = 0; i < layer; ++i) {
        cout << "----" ;
    }
    cout <<"["<<node->val<<"]"<<endl;
}

int main(){

    TreeNode a(1);
    TreeNode b(2);
    TreeNode c(5);
    TreeNode d(3);
    TreeNode e(4);
    TreeNode f(6);

    a.left = &b;
    a.right = &c;
    b.left = &d;
    b.right = &e;
    c.right = &f;

    preorder_print(&a,0);
    cout << "========================" <<endl;
    inorder_print(&a,0);
    cout << "========================" <<endl;
    postorder_print(&a,0);

    return 0;
}
foreach binary tree by pre,in,post order!

 

===========前序=============
[1]
----[2]
--------[3]
--------[4]
----[5]
--------[6]
===========中序=============
--------[3]
----[2]
--------[4]
[1]
----[5]
--------[6]
===========后序=============
--------[3]
--------[4]
----[2]
--------[6]
----[5]
[1]

例题:

例1:路径之和 (LeetCode 113)

 

思考:

 

思路:

 

 

 

 

 

代码:

#include <iostream>
#include <vector>
using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
// 前序遍历打印 二叉树 
void preorder_print(TreeNode* node,int layer){
    if (node == NULL){
        return;
    }
    for (int i = 0;i < layer; ++ i) {
        cout << "----";// 根据层数 打印 ----  
    }
    cout << "["<< node->val<< "]"<<endl;

    preorder_print(node->left,layer+1);
    preorder_print(node->right,layer+1);
}

// 打印输出 二维数组 vector<vector<int>>
void print2Dvec(const vector<vector<int>>& vec){

    for (int i = 0; i < (int)vec.size(); ++i) {
        for (int j = 0; j < (int)vec[i].size(); ++j) {
            cout << vec[i][j] << " ";
        }
        cout << endl;
    }

}


// 在 heap 上构建 一个二叉树 !
TreeNode* constructBiTree(){

    TreeNode *a = new TreeNode(1);
    TreeNode *b = new TreeNode(2);
    TreeNode *c = new TreeNode(5);
    TreeNode *d = new TreeNode(3);
    TreeNode *e = new TreeNode(4);
    TreeNode *f = new TreeNode(6);

    a->left = b;
    a->right = c;
    b->left = d;
    b->right = e;
    c->right = f;

    return a;
}


// ==================Solution=========================
class Solution{
public:
    vector<vector<int>> pathSum(TreeNode* root, int sum) {

        vector<vector<int>> result; // 存储满足条件路径的 数组
        vector<int> path;
        int path_value = 0;
        preorder(root,path_value,sum,path,result);

        return result;
    }
private:
    //                                                                              传出参数
    //            正在遍历的节点   当前路径的和   要求的和  存储当前路径的数组   满足要求的路径二维数组 
    void preorder(TreeNode* node,int& path_value,int sum,vector<int>& path,vector<vector<int>>& result){
        if (node == NULL)
            return ;
        path_value += node->val;// 每次遍历都将 node的值加到 path_value
        path.push_back(node->val);

        if ( !node->left && !node->right && path_value == sum){ // 满足该 条件,将path 加入到 result 
            result.push_back(path);
        }

        preorder(node->left,path_value,sum,path,result);
        preorder(node->right,path_value,sum,path,result);

        // 此时遍历到叶子节点 
        path_value -= node->val;
        path.pop_back();
    }

};

// ==================Solution=========================

int main(){

    // 构造二叉树 
    TreeNode* root = constructBiTree();
    //preorder_print(root,0);
    Solution* s = new Solution();
    vector<vector<int>> ret = s->pathSum(root,6);
    print2Dvec(ret);

    return 0;
}
View Code

 

posted @ 2020-09-13 21:50  Zcb0812  阅读(146)  评论(0编辑  收藏  举报