编程之旅-Day36

目录

Day36-学习内容:

1.剑指Offer

面试题26:树的子结构

面试题34:二叉树中和为某值的路径

 2.Leetcode

例1:字符串的解码方式

例2:灰色编码


1.剑指Offer

面试题26:树的子结构

题目描述:输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)

思路:递归,注意空指针,保证代码的鲁棒性。

代码:

class Solution {
public:
    bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
    {
        if(pRoot1==nullptr||pRoot2==nullptr) return false;
        bool result=false;
        if(pRoot1->val==pRoot2->val){
             result=DoTree1HaveTree2(pRoot1,pRoot2);
        }
        if(!result){
            result=HasSubtree(pRoot1->left,pRoot2);
        }
        if(!result){
            result=HasSubtree(pRoot1->right,pRoot2);
        }
        return result;
    }
    bool DoTree1HaveTree2(TreeNode* pRoot1, TreeNode* pRoot2){
        if(pRoot2==nullptr) return true;
        if(pRoot1==nullptr) return false;
        if(pRoot1->val!=pRoot2->val){
            return false;
        }
        return DoTree1HaveTree2(pRoot1->left,pRoot2->left)&&DoTree1HaveTree2(pRoot1->right,pRoot2->right);
    }
};

面试题34:二叉树中和为某值的路径

题目描述:输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的list中,数组长度大的数组靠前)

思路:前序遍历、递归,用栈保存之前的结果。

代码:

class Solution {
public:
    vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
        vector<vector<int> > res;
        vector<int> temp;
        int cursum=0;
        if(root==nullptr) return res;
        FindPathCore(root,expectNumber,cursum,temp,res);
        return res;
    }
    void FindPathCore(TreeNode* root,int expectNumber,int cursum,vector<int> &temp,vector<vector<int> > &res){
        cursum+=root->val;
        temp.push_back(root->val);
        
        bool isleaf=root->left==nullptr&&root->right==nullptr;
        if(cursum==expectNumber&&isleaf){
            res.push_back(temp);
        }
        if(root->left!=nullptr){
            FindPathCore(root->left,expectNumber,cursum,temp,res);
        }
        if(root->right!=nullptr){
            FindPathCore(root->right,expectNumber,cursum,temp,res);
        }
        temp.pop_back();
        cursum-=root->val;
    }
};

 2.Leetcode

例1:字符串的解码方式

题目描述:

A message containing letters fromA-Zis being encoded to numbers using the following mapping: 

'A' -> 1
'B' -> 2
...
'Z' -> 26

Given an encoded message containing digits, determine the total number of ways to decode it. 

For example,
Given encoded message"12", it could be decoded as"AB"(1 2) or"L"(12). 

The number of ways decoding"12"is 2.

思路:动态规划

代码:

class Solution {
public:
    int numDecodings(string s)
    {
        int size=s.length();
        if(size==0||s[0]=='0') return 0;
        vector<int> dp(size+1,0);
        dp[0]=1;
        dp[1]=1;
        for(int i=2;i<=size;i++){
            if('1'<=s[i-1]&&s[i-1]<='9'){
                dp[i]+=dp[i-1];
            }
            if(s[i-2]=='1'||(s[i-2]=='2'&&s[i-1]>='0'&&s[i-1]<='6')){
                dp[i]+=dp[i-2];
            }
        }
        return dp[size];
    }     
};

例2:灰色编码

题目描述:

The gray code is a binary numeral system where two successive values differ in only one bit. 

Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0. 

For example, given n = 2, return[0,1,3,2]. Its gray code sequence is: 

00 - 0
01 - 1
11 - 3
10 - 2

Note: 
For a given n, a gray code sequence is not uniquely defined. 

For example,[0,2,3,1]is also a valid gray code sequence according to the above definition. 

For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that. 

思路:

//随着n变大,前面的数不用动,后面的数倒着拿出来再在首部加1即可

代码:

class Solution {
public:
    vector<int> grayCode(int n) {
        vector<int> result;
        result.push_back(0);
        for (int i = 0; i < n; i++) {
            const int hight_bit = 1 << i;
            for (int j = result.size() - 1; j >= 0; j--) //第二遍反着遍历,形成对称
                result.push_back(hight_bit | result[j]);
        }
        return result;
    }
};

猜你喜欢

转载自blog.csdn.net/linyuhan3232/article/details/89480674