[] Data structure - brush diary questions about Binary Tree

1, it is determined whether a binary tree, a binary tree to another subtree

Here Insert Picture Description

class Solution
{
    bool IsSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
	{
        if (pRoot2 == NULL)//第二棵树遍历完,但是第一棵树没有遍历完返回真的
			return true;
		if (pRoot1 == NULL)//第一棵树遍历完,但是第二棵树没有遍历完返回假的 
			return false;
		if (pRoot2->val == pRoot1->val)
		{
			return IsSubtree(pRoot1->left, pRoot2->left)
				&& IsSubtree(pRoot1->right, pRoot2->right);
		}
		else    return false;
	}
public:
		bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
	{
		if (pRoot1 == NULL || pRoot2 == NULL)
			    return false;
		return   IsSubtree(pRoot1, pRoot2)
			|| HasSubtree(pRoot1->left, pRoot2)
			|| HasSubtree(pRoot1->right, pRoot2);
	}
};

2, the binary image

Here Insert Picture Description

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    void Mirror(TreeNode *pRoot) 
    {
        if(pRoot==NULL)
            return;
        if(pRoot->left==NULL&&pRoot->right==NULL)
            return ;
        TreeNode *tmp=pRoot->left;
        pRoot->left=pRoot->right;
        pRoot->right=tmp;
        if(pRoot->left!=NULL)
            Mirror(pRoot->left);
        if(pRoot->right!=NULL)
            Mirror(pRoot->right);
    }
};

3, printing each node of the binary tree

Here Insert Picture Description
Thinking: traversal sequence

Code:

class Solution {
public:
    //二叉树的层序遍历
    vector<int> PrintFromTopToBottom(TreeNode* root) 
    {
        vector<int> v;
        queue<TreeNode*> q;
        if(root==NULL)
            return v;
        q.push(root);
        while(!q.empty())
        {
            TreeNode* ret=q.front();
            q.pop();
            if(ret)
            {
                v.push_back(ret->val);
                q.push(ret->left);
                q.push(ret->right);
            }
        }
        return v;
    }
};

4, a binary tree and a path to a value of

Here Insert Picture Description
NOTE: Be sure to the leaf nodes, the path is not only part of
the code:

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution 
{
        vector<int> v;
        vector<vector<int>> res;
    void Find(TreeNode* root,int expectNumber)
    {
        if(root==NULL)
            return;
        v.push_back(root->val);
        if(!root->left&&!root->right&&root->val==expectNumber)
            res.push_back(v);
        else
        {
            if(root->left)
                Find(root->left,expectNumber-root->val);
            if(root->right)
                Find(root->right,expectNumber-root->val);
        }
        v.pop_back();
    }
public:
    vector<vector<int> > FindPath(TreeNode* root,int expectNumber) 
    {
      Find(root,expectNumber);
      return res;
    }
};

5, Binary Tree and doubly linked list

Here Insert Picture Description

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution 
{
    TreeNode* head=nullptr;
    TreeNode* realhead=nullptr;
    void ConvertHelp(TreeNode* pRootOfTree)
    {
        if(pRootOfTree==NULL)
            return;
        ConvertHelp(pRootOfTree->left);
        if(head==nullptr)
        {
            head=pRootOfTree;
            realhead=pRootOfTree;
        }
        else
        {
           //变成一个双向链表   
            head->right=pRootOfTree;
            pRootOfTree->left=head;
            head=pRootOfTree;
        }
        ConvertHelp(pRootOfTree->right);
    }
public:
    TreeNode* Convert(TreeNode* pRootOfTree)
    {
        if(pRootOfTree==NULL)
            return NULL;
        ConvertHelp(pRootOfTree);
        return realhead;
    }
};

6, clockwise print matrix (this problem and has nothing to do binary tree)

Here Insert Picture Description

class Solution {
public:
	vector<int> printMatrix(vector<vector<int> > matrix)
	{
		vector<int> temp;
		int col = matrix[0].size();  //列
		int row = matrix.size(); //行
		if (row == 0 || col == 0)
			return temp;
		int top = 0;
		int right = col - 1;
		int left = 0;
		int bottom = row - 1;
		while (top <= bottom && left <= right)
		{
			//右
			for (int i = left; i <= right; i++)
			{
				temp.push_back(matrix[top][i]);
			}
			//下
			for (int i = top + 1; i <= bottom; i++)
			{
				temp.push_back(matrix[i][right]);
			}
			//左
			for (int i = right - 1; i >= left && top < bottom; i--)
			{
				temp.push_back(matrix[bottom][i]);
			}
			//上
			for (int i = bottom - 1; i > top&&right > left; i--)
			{
				temp.push_back(matrix[i][top]);
			}
			bottom--; right--; left++; top++;
		}
		return temp;
	}
};
Published 42 original articles · won praise 13 · views 1766

Guess you like

Origin blog.csdn.net/Vicky_Cr/article/details/105248093