LeetCode 二叉树基本题目总结

完成本博文中提到的二叉树题目,可以获得对二叉树结构的更深刻的认识

Leetcode105 重建二叉树

题目要求根据一棵二叉树的前序遍历结果和中序遍历结果,根据两个二叉树遍历结果重新构建二叉树。

整体思路就是:根据前序遍历确定根节点,根据中序遍历确定左右子树的元素,然后对前序遍历数组和中序遍历数组进行划分,递归地重建二叉树

TreeNode* dfs(TreeNode* root,vector<int>& preorder, vector<int>& inorder){
        if(preorder.size()==0)   return NULL;
        root=new TreeNode(preorder[0]);
        vector<int> newprel,newprer,newinl,newinr;
        int i,j;
        for(i=0;i<inorder.size();i++){            
            if(inorder[i]==root->val)
                 break;
            else
                newinl.push_back(inorder[i]);
         }        
         for(i=i+1;i<inorder.size();i++)
             newinr.push_back(inorder[i]);
         int k=newinl.size();
         for(i=1;i<preorder.size();i++){
              if(i<=k)
                 newprel.push_back(preorder[i]);
              else
                 newprer.push_back(preorder[i]);
         }
         root->left=dfs(root->left,newprel,newinl);
         root->right=dfs(root->right,newprer,newinr);
         return root;
    }
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) { 
        TreeNode* root;
        root=dfs(root,preorder,inorder);
        return root;
    }

LeetCode 98 验证二叉搜索树

这个是非常常规的题目,基本的做法有两种:

  • 深度优先遍历,通过向下传递一个合法节点值的区间,来判断当前节点是否是合法节点
  • 二叉树的中序遍历,返回中序遍历序,判断是否单调递增
  • 面试时面试官往往会要求给出非递归的中序遍历实现

以下是中序遍历非递归的做法:

    bool isValidBST(TreeNode* root) {
            if(root==NULL) return true;
            stack<TreeNode*> path;
            vector<int> inorder;
            TreeNode *p=root;
            while(p || !path.empty()){
                while(p){
                    path.push(p);
                    p=p->left;
                   }
                p=path.top();
                path.pop();
                inorder.push_back(p->val);
                p=p->right;
               }
            for(int i=1;i<inorder.size();i++){
                if(inorder[i-1]>=inorder[i])
                     return false;
            }
            return true;
     }

Leetcode199 二叉树的右视图

若二叉树如下,则返回【2,3,4】,即返回每一层的最右边节点

    2
   / \
   1  3
  /
 4 

这显示是一道宽度优先遍历的问题,所以使用宽度优先遍历即可

    vector<int> rightSideView(TreeNode* root) {
        vector<int> ans;
        if(!root) return ans;
        TreeNode *p=root;
        int size;
        queue<TreeNode*> level;
        level.push(p);
        while(!level.empty()){
            size=level.size();
            ans.push_back(level.back()->val);
            for(int i=0;i<size;i++){
                p=level.front();
                level.pop();
                if(p->left) level.push(p->left);
                if(p->right) level.push(p->right);
            }
        }
        return ans;
    }

Leetcode 124 二叉树中的最大路径和

给定一个非空二叉树,返回其最大路径和。本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列。该路径至少包含一个节点,且不一定经过根节点。
看到题目最直接的想法是需要使用深度优先搜索,但是问题来了,题目要求的是返回最大路径和,而不是最大深度和,所以DFS的使用需要一些灵活地改变

  • 使用maxlen记录当前最大路径和
  • 当访问一个节点时,该节点的路径和为节点值+左路径+右路径,将路径和与maxlen比较更新
  • 向上返回时返回一条包含节点值的最大路径(左路径+节点或右路径+节点)
class Solution {
public:
    int maxlen=INT_MIN;
    int dfs(TreeNode* root){
        if(root==NULL) return 0;
        if(!root->left && !root->right)
        {
            maxlen=maxlen<root->val?root->val:maxlen;
            return max(root->val,0);
        }
        int left=dfs(root->left),right=dfs(root->right);
        maxlen=maxlen<(left+right+root->val)?(root->val+left+right):maxlen;
        return max(max(left+root->val,0),right+root->val);
    }
    int maxPathSum(TreeNode* root) {
        if(!root) return 0;
        int len=dfs(root);
        return maxlen;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值