zoukankan      html  css  js  c++  java
  • 树的简单操作

    struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    };

    //树相同
    bool isSameTree(TreeNode* p, TreeNode* q) {
    if (p == nullptr && q == nullptr)
    return true;
    if (p == nullptr || q == nullptr || p->val != q->val)
    return false;

    return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);

    }

    //树 镜像对称
    bool isTheSame(TreeNode* p, TreeNode *q)
    {
    if (p == nullptr && q == nullptr)
    return true;
    if (p == nullptr || q == nullptr || p->val != q->val)
    return false;

    return isTheSame(p->left, q->right) && isTheSame(p->right, q->left);

    }
    bool isSymmetric2(TreeNode* root) {
    if (root == nullptr)
    return true;

    return isTheSame(root->left, root->right);
    }

    //迭代的方式——非递归
    bool isSymmetric(TreeNode* root) {
    if (root == nullptr)
    return true;
    stack<TreeNode*> s;
    TreeNode* p ;
    TreeNode* q ;

    s.push(root->left);
    s.push(root->right);

    while (!s.empty())
    {
    p = s.top(); s.pop();
    q = s.top(); s.pop();

    if (p == nullptr && q == nullptr)
    continue;
    if (p == nullptr || q == nullptr || p->val != q->val)
    return false;

    s.push(p->left); s.push(q->right);
    s.push(p->right); s.push(q->left);
    }

    return true;
    }

    //返回自底向上的层次遍历
    void findnext(unordered_map<int, vector<int>> &map,TreeNode *p,int depth=0)
    {
    if (p == nullptr)
    return;

    map[depth].push_back(p->val);
    findnext(map, p->left, depth + 1);
    findnext(map, p->right, depth + 1);
    }
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
    unordered_map<int ,vector<int>> map;
    vector<vector<int>> result;
    findnext(map, root, 0);

    for (int i = map.size() -1; i >= 0; i--)
    result.push_back(map[i]);

    return result ;
    }

    //将排序数组转化为 高度平衡二叉树
    TreeNode* createTree(vector<int> &nums,int begin,int end)
    {

    if (begin > end)
    return nullptr;

    if(begin == end)
    return new TreeNode(nums[begin]);


    int middle = (begin + end) >> 1;
    TreeNode* p = new TreeNode(nums[middle]);
    p->left = createTree(nums, begin, middle - 1);
    p->right = createTree(nums, middle + 1, end);

    return p;
    }
    TreeNode* sortedArrayToBST(vector<int>& nums) {
    return createTree(nums, 0, nums.size() - 1);
    }

    void printTree(TreeNode* root,int depth =0)
    {
    if (root == nullptr)
    return;

    for (int i = 0; i < depth; i++)
    cout << " ";
    cout << root->val << endl;

    printTree(root->left, depth + 1);
    printTree(root->right, depth + 1);
    }
    //*****************************************

    // 判断是否存在从根到叶子节点的 路径和 等于 指定目标和 -----并打印出路径
    vector<vector<int>> result;
    void path(vector<int> tmp,TreeNode* p, int target, int current)
    {
    if (p == nullptr)
    return ;
    tmp.push_back(p->val);
    //加上当前节点的权值
    current += p->val;

    //符合要求,输出True
    if (current == target && p->left == nullptr && p->right == nullptr)
    {
    result.push_back(tmp);
    return ;
    }

    path(tmp, p->left, target, current);
    path(tmp,p->right, target, current);

    }

    vector<vector<int>> pathSum(TreeNode* root, int sum) {
    vector<int> tmp;
    path(tmp, root, sum, 0);
    return result;
    }

    //****************************************

    struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}

    };
    int rmax = 0;
    int find_depth(TreeNode* p, int value)
    {
    if (p == nullptr || p->val != value)
    return 0;

    int left = 0, right = 0;
    left = find_depth(p->left,value);
    right = find_depth(p->right,value);

    return 1 + Max(left, right);

    }
    void solution(TreeNode* p)
    {
    if (p == nullptr)
    return;

    int current = find_depth(p->left, p->val) + find_depth(p->right, p->val);
    if (current > rmax)
    rmax = current;
    solution(p->left);
    solution(p->right);
    }


    int longestUnivaluePath(TreeNode* root) {
    solution(root);
    return rmax;
    }

    void printTree(TreeNode* p,int depth=0)
    {
    if (p == nullptr)
    return;

    for (int i = 0; i < depth; i++)
    cout << " ";
    cout << p->val << endl;

    printTree(p->left,depth+1);
    printTree(p->right, depth+1);
    }

    //*************中序遍历*************
    void find_next(vector<int> &vi, TreeNode *p)
    {
    if (p == nullptr)
    return;

    find_next(vi,p->left);
    vi.push_back(p->val);
    find_next(vi, p->right);

    }
    vector<int> inorderTraversal(TreeNode* root) {
    vector<int> result;

    find_next(result ,root);
    return result;
    }
    //求不同的搜索树有多少种类

    int numTrees(int n) {
    if (n < 2)
    return 1;
    vector<int> v(n+1, 0);
    v[0] = 1;
    v[1] = 1;

    for (int i = 2; i <= n; i++)
    {
    for (int j = 0; j < i; j++)
    v[i] += v[j] * v[i - 1 - j];
    }
    return v[n];
    }
    //求不同的搜索树 并输出
    vector<TreeNode*>* create_tree(int left,int right)
    {
    vector<TreeNode*> *result = new vector<TreeNode*>;
    if (left > right)
    {
    result->push_back(nullptr);
    }
    else
    {
    for (int i = left; i <= right; i++)
    {
    vector<TreeNode*> *lTrees = create_tree(left, i - 1);
    vector<TreeNode*> *rTrees = create_tree(i+1, right);
    for (int j = 0; j < lTrees->size(); j++)
    {
    for (int k = 0; k < rTrees->size(); k++)
    {
    TreeNode* tmp = new TreeNode(i);
    tmp->left = (*lTrees)[j];
    tmp->right = (*rTrees)[k];
    result->push_back(tmp);
    }
    }
    }
    }
    return result;
    }
    vector<TreeNode*> generateTrees(int n) {
    if (n < 1)
    return vector<TreeNode*>();

    return *create_tree(1, n);
    }

  • 相关阅读:
    HDU 1114 Piggy-Bank
    HDU 2955 Robberies
    NTOJ 290 动物统计(加强版)
    POJ 3624 Charm Bracelet
    HDU 2602 Bone Collector
    POJ 1523 SPF(无向图割顶)
    HDU 5311 Hidden String
    HDU 1421 搬寝室
    HDU 1058 Humble Numbers
    POJ 3259 Wormholes(spfa判负环)
  • 原文地址:https://www.cnblogs.com/Oscar67/p/9430626.html
Copyright © 2011-2022 走看看