二叉树的面试题集锦(五)

1.打印所有路径

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<string> binaryTreePaths(TreeNode* root) 
    {
        vector<string> Vector;
        string str;
        BinaryTreePaths(Vector,str,root);
        return Vector;
    }
    void BinaryTreePaths(vector<string>&Vector,string str,TreeNode* root)
    {
        if(root)
        {
            str = str==""?to_string(root->val):str+"->"+to_string(root->val);
            if(root->left == NULL && root->right == NULL)
            Vector.push_back(str);
            BinaryTreePaths(Vector,str,root->left);
            BinaryTreePaths(Vector,str,root->right);
        }
    }
};

 

2.Sum Root to Leaf Numbers

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int sumNumbers(TreeNode* root) 
    {
        int SumOnePath = 0,SumAllPath = 0;
        SumNumbers(root,SumOnePath,SumAllPath);
        return SumAllPath;
    }
    void SumNumbers(TreeNode* root,int& SumOnePath,int& SumAllPath)
    {
        if(root)
        {
            SumOnePath = SumOnePath*10 +root->val;
            if(root->left == NULL && root->right == NULL)
            {
                SumAllPath+=SumOnePath;  
                SumOnePath/=10;
                return;
            }
            SumNumbers(root->left,SumOnePath,SumAllPath);
            SumNumbers(root->right,SumOnePath,SumAllPath);
            SumOnePath/=10;
        }
    }
};

 

 

3.和为某一值的一条路径

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool hasPathSum(TreeNode* root, int sum) 
    {
        vector<int> Vector;
         return FindPathOfSum(root,Vector,sum);
    }
    
    int Add(vector<int> Vector)
    {
        int Sum = 0;
        for (int i = 0; i < Vector.size(); ++i)
        {
            Sum += Vector[i];
        }
        return Sum;
    }

    void Show(vector<int> Vector)
    {
        for (int i = 0; i < Vector.size()-1; ++i)
        {
            cout << Vector[i] << "->";
        }
        cout << Vector[Vector.size() - 1];
    }


    bool FindPathOfSum(TreeNode* root, vector<int>& Vector, int sum)
    {
        if (root)
        {
            Vector.push_back(root->val);
            if (root->left == NULL && root->right == NULL && sum == Add(Vector))
            {
                Show(Vector);
                return true;
            }
            if (FindPathOfSum(root->left, Vector, sum))
            {
                return true;
            }
            else
            {
                bool Symbol = FindPathOfSum(root->right, Vector, sum);
                if (Symbol == false)
                    Vector.pop_back();
                return Symbol;
            }
    
        }
        return false;
    }
};

 

 

4.和为某一值的所有路径

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
 int Sum(vector<int>& Vector)
 {
     int Sum=0;
     for(int i=0;i<Vector.size();++i)
     {
         Sum+=Vector[i];
     }
     return Sum;
 }
class Solution 
{
public:
    vector<vector<int>> pathSum(TreeNode* root, int sum) 
    {
        vector<vector<int>> VectorResult;
        vector<int> VectorTmp;
        PathSum(root,VectorResult,VectorTmp,sum);
        return VectorResult;
    }
    void PathSum(TreeNode* root,vector<vector<int>>& VectorResult,vector<int> VectorTmp,int sum)
    {
        if(root)
        {
            VectorTmp.push_back(root->val);
            if(root->left == NULL && root->right == NULL && sum == Sum(VectorTmp))
            VectorResult.push_back(VectorTmp);
            PathSum(root->left,VectorResult,VectorTmp,sum);
            PathSum(root->right,VectorResult,VectorTmp,sum);
        }
    }
};




/*剑指offer*/
    /*打印出所有的和为某一值的路径*/
    void PathNum(Node* root, int sum)
    {
        vector<int> Vector;
        int Tmp = 0;
        _PathNum(root, Vector,Tmp, sum);
    }
    void _PathNum(Node* root, vector<int> Vector,int Tmp, int sum)
    {
        if (root)
        {
            Vector.push_back(root->_value);
            Tmp += root->_value;
            if (root->_LeftChild == NULL && root->_RightChild == NULL && Tmp == sum)
            {
                for (int i = 0; i < Vector.size() - 1; ++i)
                {
                    cout << Vector[i] << "->";
                }
                cout << Vector[Vector.size() - 1] << endl;
            }
            _PathNum(root->_LeftChild, Vector, Tmp, sum);
            _PathNum(root->_RightChild, Vector, Tmp, sum);
        }
    }

 

 

5.判断子结构

 

bool HasSubTree(Node* root1, Node* root2)
{
    bool result = false;
    if (root1 && root2)
    {
    if (root1->_value == root2->_value)
        result = HaveSubTree(root1, root2);
    if (!result)
        result = HasSubTree(root1->_LeftChild, root2);
    if (!result)
        result = HasSubTree(root1->_RightChild, root2);
    }
    return result;
}


template<class T>
bool BinaryTree<T>::HaveSubTree(Node* root1, Node* root2)
{
    if (root2 == NULL)
        return true;
    if (root1 == NULL)
        return false;
    if (root1->_value == root2->_value)
        return HaveSubTree(root1->_LeftChild, root2->_LeftChild) && 
        HaveSubTree(root1->_RightChild, root2->_RightChild);
    else
        return false;
}

 

posted @ 2016-05-18 16:14  _in_the_way  阅读(132)  评论(0)    收藏  举报