二叉树

来源:互联网 发布:影子网络怎么进入 编辑:程序博客网 时间:2024/04/30 12:31
考虑到面试,便将一些知识整理一下,温故而知新。二叉树,链表,图,贪心,动规,数组,哈希表……
先看看二叉树吧。
摘自剑指offer。
1.(第六题)重建二叉树
BinaryTreeNode/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) { TreeNode root =re(pre,0,pre.length-1,in,0,in.length-1);//传入前序遍历和中序遍历的序列,返回还原的二叉树。        return root;    }   public TreeNode re(int[] pre,int startPre,int endPre,int[] in,int startIn,int endIn);   {        if(startPre>endPre||startIn>endIn)        {//判定是否序列是否便利完。            return null;        }        TreeNode root =new TreeNode(pre[startPre]);//存入节点        for(int i=startIn;i<=endIn;i++)        {//从中序遍历开始,寻找和根节点相同的元素。            if(in[i]==pre[startPre])            {//找到了之后分为左右子树,递归进行查找。                root.left=re(pre,startPre+1,startPre+i-startIn,in,startIn,i-1);                root.right=re(pre,startPre+i-startIn+1,endPre,in,i+1,endIn);            }        }        return root;    }};
2.判断b是不是a的子结构
/*struct TreeNode {int val;struct TreeNode *left;struct TreeNode *right;TreeNode(int x) :val(x), left(NULL), right(NULL) {}};*/class Solution {public:    bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)    {        bool result= false;            if(pRoot1!=NULL&&pRoot2!=NULL)                {                if(pRoot1->val==pRoot2->val)                    result=TestTree(pRoot1,pRoot2);                if(!result)                    result=HasSubtree(pRoot1->left,pRoot2);                if(!result)                    result=HasSubtree(pRoot1->right,pRoot2);            }          return result;    }    bool TestTree(TreeNode* pRoot1, TreeNode* pRoot2)        {        if(pRoot2==NULL)            return true;        if(pRoot1==NULL)            return false;        if(pRoot1->val!=pRoot2->val)            return false;        return TestTree(pRoot1->left,pRoot2->left)&&TestTree(pRoot1->right,pRoot2->right);    }};
3.二叉树镜像(通过率20%)
/*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 *pTemp=pRoot->left;            pRoot->left=pRoot->right;            pRoot->right=pTemp;        if(pRoot->right)            Mirror(pRoot->right);        if(pRoot->left)            Mirror(pRoot->left);    }};
4.从上到下打印二叉树
/*struct TreeNode {int val;struct TreeNode *left;struct TreeNode *right;TreeNode(int x) :val(x), left(NULL), right(NULL) {}};*/class Solution {public:    vector<int> PrintFromTopToBottom(TreeNode* root) {if(!root)                return;        std::deque<BinaryTreeNode*>dequeTreeNode;        dequeTreeNode.push_back(root);            while(dequeTreeNode.size())                {                TreeNode *pNode=dequeTreeNode.front();                dequeTreeNode.pop_front();                printf("%d",pNode->val);                if(pNode->left)                    dequeTreeNode.push_back(pNode->left);                if(pNode->right)                    dequeTreeNode.push_back(pNode->right);            }    }};
5.判断某数组是不是二叉便利书的后序遍历结果
  
class Solution {public:    bool VerifySquenceOfBST(vector<int> sequence) {       if(sequence.length==0) return false;          return checkBST(sequence, 0, sequence.length-1);      }       public boolean checkBST(int[] sequence,int start,int end){               if(start>=end) return true;          int flag = sequence[end];          int i=start;          for(;i<=end;i++){              if(sequence[i]>=flag) break;                  }          int j=end-1;          for(;j>=start;j--){              if(sequence[j]<=flag) break;                  }                if(i-j!=1) return false;          return checkBST(sequence, start, i-1)                  &&checkBST(sequence, j+1, end-1);        }  };
6.输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径
/*struct TreeNode {int val;struct TreeNode *left;struct TreeNode *right;TreeNode(int x) :val(x), left(NULL), right(NULL) {}};*/class Solution {public:    vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {         if(root==NULL)             return;        std::vector<int>path;        int currentSum=0;        FindPath(root,expectNumber,path,currentSum);         }    void FindPath(TreeNode* root,int expectSum,std::vector<int> &path, int currentSum)        {            }};






0 0