树的相关算法

1.Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
The left subtree of a node contains only nodes with keys less than the node’s key.
The right subtree of a node contains only nodes with keys greater than the node’s key.
Both the left and right subtrees must also be binary search trees.
这里写图片描述

树的定义:

  struct TreeNode {
      int val;
      TreeNode *left;
      TreeNode *right;
      TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  };

解题思想:
1.二叉搜索树上的每一个节点的左子树均小于该节点,并且右子树上的每一个节点都大于该节点,(注意判断的时候不是说节点的左节点小于,右节点大于该节点就行了,而是左(右)子树上的每一个节点均小(大)于该节点),用递归可以解决,采用深搜。
2.二叉搜索树的中序遍历是升序,可以判断搜索树中序遍这里写代码片历序列。

//暴力遍历
class Solution {
public:
    bool isValidBST(TreeNode* root) {
        if(root==NULL)
            return true;
        if(!DFS_left(root->left,root->val)||!DFS_right(root->right,root->val))
            return false;
        return isValidBST(root->left)&&isValidBST(root->right);
    }
    bool DFS_left(TreeNode* root,int val){
        if(root==NULL)
            return true;
        if(root->val>=val)
            return false;
        return DFS_left(root->left,val)&&DFS_left(root->right,val);
    }
    bool DFS_right(TreeNode* root,int val){
        if(root==NULL)
            return true;
        if(root->val<=val)
            return false;
        return DFS_right(root->left,val)&&DFS_right(root->right,val);
    }
    }
 //判断中序序列是否为升序
class Solution {
 vector<TreeNode*> tmp;
    bool isValidBST(TreeNode* root) {
        if(root==NULL)
            return true;
        LDR_BST(root);
        for(int i=0;i<tmp.size()-1;i++)
            if(tmp[i]->val>=tmp[i+1]->val)
                return false;
        return true;
    }
    void LDR_BST(TreeNode* root){
        if(root==NULL)
            return;
        LDR_BST(root->left);
        tmp.push_back(root);
        LDR_BST(root->right);
    }
};

2.Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake.
Recover the tree without changing its structure.

在一个二叉搜索树中有两个树节点的位置反了,要求复原二叉搜索树。
解题思想:有了前题中序遍历的思想。我们可以通过中序搜索序列来还原二叉搜索树。

当有两个树节点交换的情况下,树的中序遍历序列会产生两种情况:
1) 被交换的两个结点相邻,如124356,这样只需要把相邻的3和4交换回来即可;2) 被交换的两个结点不相邻,如163452,这样我们需要找出两个逆序的地方,63和52,并交换第一个逆序的前者和第二个逆序的后者。

class Solution {
public:
    vector<TreeNode*> tmp;
    void recoverTree(TreeNode* root) {
        if(root==NULL)
            return;
        DFS_BST(root);
        bool found=false;
        TreeNode *p,*q;
        for(int i=0;i<tmp.size()-1;i++)
        {
            if(tmp[i]->val>=tmp[i+1]->val){
                q=tmp[i+1];
                if(found==false){
                    p=tmp[i];
                    found=true;
                }
            }
        }
        int t=p->val;
        p->val=q->val;
        q->val=t;
    }
    void DFS_BST(TreeNode* root){
        if(root==NULL)
            return ;
        DFS_BST(root->left);
        tmp.push_back(root);
        DFS_BST(root->right);
    }
};

3.Same Tree

class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        return DFS(p,q);
    }
    bool DFS(TreeNode* p, TreeNode* q){
        if(p==NULL&&q==NULL)
            return true;
        if(p==NULL||q==NULL||p->val!=q->val)
            return false;    
        return DFS(p->left,q->left)&&DFS(p->right,q->right);
    }
};

4.Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

这里写图片描述

解题思想:对称二叉树又一个特点就是每一个节点的左子树左节点和右子树的右节点相等,左子树的右节点和右子树的左节点相等。
通过这个特征我们可以判断一个树是否关于根节点对称。

class Solution {
public:
    //vector<TreeNode*> tmp;
    bool isSymmetric(TreeNode* root) {
        if(root==NULL)
            return true;
        return _helper(root->left,root->right);
    }
    bool _helper(TreeNode* left,TreeNode* right){
        if(left==NULL&&right==NULL)
            return true;
        if(left==NULL||right==NULL||left->val!=right->val)
            return false;

        return _helper(left->left,right->right)&&_helper(left->right,right->left);   //对称树的判别方式
    }
};

5.Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).
For example:
Given binary tree [3,9,20,null,null,15,7],
这里写图片描述

解题思想:通过一个两端队列我们就可以对一棵树实现广度优先搜索,
解决本题我们可以通过再加一个双端队列来存储每一层的树节点。

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> rst;
        if(root==NULL)
            return rst;
        deque<TreeNode*> q1;
        q1.push_back(root);
        while(q1.size()!=0){     //广度优先搜索
            deque<TreeNode*> q2;
            vector<int> tmp;
            while(q1.size()!=0){
                TreeNode* t=q1.front();
                q1.pop_front();
                tmp.push_back(t->val);
                if(t->left)
                    q2.push_back(t->left);
                if(t->right)
                    q2.push_back(t->right);
            }
            rst.push_back(tmp);
            q1=q2;
        }
        return rst;
    }
};

6.Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

求树的深度,简单递归深搜就可以解决了。

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(root==NULL)
            return 0;
        else
            return max(maxDepth(root->left),maxDepth(root->right))+1;
    }
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值