每日5题

(1)树的镜像对称判断:

递归算法:

//Definition for a binary tree node
struct TreeNode{
    int val;
    TreeNode * left;
    TreeNode * right;
    TreeNode(int x) :val(x), left(NULL), right(NULL){}
};
//判断二叉树镜像对称
bool isSymmetric(TreeNode * root){
    if (!root){
        return true;
    }
    return dfs(root->left, root->right);
}
bool dfs(struct TreeNode * left, struct TreeNode * right){
    if (!left&&!right){
        return true;
    }
    if (!left || !right){
        return false;
    }
    if (left->val == right->val){
        return dfs(left->left, right->right) && dfs(left->right, right->left);
    }
}

(2)计算二叉树的所有左叶子节点的和

//Definition for a binery tree node
struct TreeNode{
    int val;
    TreeNode * left;
    TreeNode *right;
    TreeNode(int x) :val(x), left(NULL), right(NULL){}
};
//计算二叉树所有左叶子节点的和,默认一个根节点,不作为左叶子节点计算在内
int ans = 0;
int sumOfLeftLeaves(TreeNode * root){
    if (!root){ return 0; }
    dfs(root, 0);
    return ans;
}
void dfs(TreeNode * root, int isLeft){
    if (!root){ return; }
    if (!root->left&&!root->right&&isLeft){
        ans += root->val; return;
    }
    dfs(root->left, 1);
    dfs(root->right, 0);
}

(3)蛇形矩阵

/*
输入两个整数n和m,输出一个n行m列的矩阵,将数字 1 到 n*m 按照回字蛇形填充至矩阵中。
1 2 3
8 9 4
7 6 5
*/
#include<iostream>
using namespace std;
//全局变量初值为0
int res[999][999];

int main(){
    int n, m;
    cin >> n >> m;
    int fx[4] = { 0, 1, 0, -1 };//定义方向,fx为铅直方向,fy为水平方向。
    int fy[4] = { 1, 0, -1, 0 };//右,下,左,上
    int  a, b;
    for (int i = 1, x = 0, y = 0, f = 0; i <= n*m; i++){
        res[x][y] = i;
        //更新下一个位置,这里是保存原先的位置,求下一个位置
        a = x + fx[f];
        b = y + fy[f]; 
        //判断边界
        if (a > n - 1 || a<0 || b>m - 1 || b < 0 || res[a][b]){
            f = (f + 1) % 4;
            a = x + fx[f];//新的方向的下一个位置
            b = y + fy[f];
        }
        x = a;
        y = b;
    }
    for (int i = 0; i < n; i++){
        for (int j = 0; j < m; j++){
            cout << res[i][j] << " ";
        }
        cout << endl;
    }
    system("pause");
    return 0;
}

(4)二维动态数组的使用

vector<vector<int> >arr;

arr.size():得到二维动态数组的行数,arr[i].size():得到二维数组的列数,arr.empty():判断二维数组是否为空。

//特殊二维动态数组的元素查找
bool searchArray(vector<vector<int> >array, int target){
    if (array.empty() || array[0].empty()){ return false; }
    int i =0, j = array[0].size()-1;
    while (i < array.size() && j >= 0){
        if (array[i][j] == target){
            return true;
        }
        if (array[i][j]>target){
            j--;
        }
        else{
            i++;
        }
    }
    return false;
}

(5)判断树的子结构

//判断树的子结构
//Definition for a binary tree node
struct TreeNode{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) :val(x), left(NULL), right(NULL){}
};
bool hasSubTree(struct TreeNode * proot1, struct TreeNode * proot2){
    if (!proot1 || !proot2){
        return false;
    }
    if (isSame(proot1,proot2)){
        return true;
    }
    return hasSubTree(proot1->left, proot2) || hasSubTree(proot1->right, proot2);
}
bool isSame(struct TreeNode * proot1,struct TreeNode * proot2){
    if (!proot2){
        return true;
    }
    if (!proot1 || proot1->val != proot2->val){
        return false;
    }
    return isSame(proot1->left, proot2->left) && isSame(proot1->right, proot2->right);
}

 

posted @ 2020-09-19 13:36  goldstine  阅读(73)  评论(0)    收藏  举报