leetcode 关于二叉树的一些题目 C++

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

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

};
/***************************************************
*  函数功能:Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
*  参数说明
*       输入参数:Given the below binary tree and sum = 22,
                          5
                         / \
                        4   8
                       /   / \
                      11  13  4
                     /  \    / \
                    7    2  5   1
*       输出参数:
                [
                   [5,4,11,2],
                   [5,8,4,5]
                ]
*  复杂性分析:时间复杂度:O(),空间复杂度:O()
*  题目来源  :https://leetcode.com/problems/path-sum-ii/description/
*  日期:2018-07-22-21.19
***************************************************/
class Solution1
{
public:
    vector<vector<int> > pathSum(TreeNode* root,int sum)
    {
        vector<int> item;
        vector<vector<int> > result;
        if(root==NULL) return result;
        int target_sum=0;
        pathSum(root,sum,item,result,target_sum);

        return result;
    }
private:
    void pathSum(TreeNode* root,int sum,vector<int>& item,vector<vector<int> >& result,int target_sum)
    {
        if(root==NULL)
        {
            return ;
        }
        item.push_back(root->val);
        target_sum+=root->val;
        if(target_sum==sum && root->left==NULL && root->right==NULL)
        {
            result.push_back(item);
        }
        pathSum(root->left,sum,item,result,target_sum);

        pathSum(root->right,sum,item,result,target_sum);
        item.pop_back();
        target_sum-=root->val;

    }
};

/***************************************************
*  函数功能:236. Lowest Common Ancestor of a Binary Tree
*  参数说明
*       输入参数:root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
*       输出参数:3 The LCA of of nodes 5 and 1 is 3.
*  复杂性分析:时间复杂度:O(),空间复杂度:O()
*  题目来源  :https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/
*  日期:2018-07-22-21.19
***************************************************/
class Solution2
{
public:
    TreeNode* lowestCommonAncestor(TreeNode* root,TreeNode* p,TreeNode* q)
    {
        if(root==NULL || p==NULL || q==NULL)
        {
            return NULL;
        }
        vector<TreeNode*> item;
        vector<TreeNode*> vec_p;
        get_path(root,p,vec_p,item);
        item.clear();
        vector<TreeNode*> vec_q;
        get_path(root,q,vec_q,item);
        int length=vec_p.size()>vec_q.size()?vec_q.size():vec_p.size();
        TreeNode* result=NULL;
        for(int i=0;i<length;i++)
        {
            if(vec_p[i]==vec_q[i])
            {
                result=vec_p[i];
            }else
            {
                break;
            }

        }
        return result;
    }

private:

    void get_path(TreeNode* root,TreeNode* target,vector<TreeNode*>& result,vector<TreeNode*>& item)
    {
        if(root==NULL)
        {
            return ;
        }
        item.push_back(root);
        if(root==target)
        {
            result=item;
            return ;
        }
        get_path(root->left,target,result,item);
        get_path(root->right,target,result,item);
        item.pop_back();
    }

};

/***************************************************
*  函数功能:Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place.
*  参数说明
*       输入参数:
                    1
                   / \
                  2   5
                 / \   \
                3   4   6
*       输出参数:
                    1
                     \
                      2
                       \
                        3
                         \
                          4
                           \
                            5
                             \
                              6
*  复杂性分析:时间复杂度:O(),空间复杂度:O()
*  题目来源  : https://leetcode.com/problems/flatten-binary-tree-to-linked-list/description/
*  日期:2018-07-22-22.06
***************************************************/
class Solution3
{
public:
    void flatten_new(TreeNode* root)
    {
        if(root==NULL) return ;
        TreeNode* pre=NULL;
        flatten_new(root,pre);
    }
    void flatten_new(TreeNode* root,TreeNode*& pre)
    {
        if(root==NULL)
        {
            return ;
        }
        flatten_new(root->right,pre);
        flatten_new(root->left,pre);
        root->right=pre;
        root->left=NULL;
        pre=root;
    }

    void flatten(TreeNode* root)
    {
        if(root==NULL) return ;
        vector<TreeNode*> result;
        flatten(root,result);
        for(int i=1;i<result.size();i++)
        {
            result[i-1]->left=NULL;
            result[i-1]->right=result[i];
        }
    }

private:
    void flatten(TreeNode* root,vector<TreeNode*>& result)
    {
        if(root==NULL)
        {
            return ;
        }
        result.push_back(root);
        flatten(root->left,result);
        flatten(root->right,result);
    }
};

void print_Tree(TreeNode* root,int layer)
{
    if(root==NULL)
    {
        return ;
    }
    for(int i=0;i<layer;i++)
    {
        cout<<"--";
    }
    cout<<root->val<<endl;
    print_Tree(root->left,layer+1);
    print_Tree(root->right,layer+1);
}

 

/***************************************************
*  函数功能:Binary Tree Right Side View
*  参数说明
*       输入参数:[1,2,3,null,5,null,4]
                   1            <---
                 /   \
                2     3         <---
                 \     \
                  5     4       <---
*       输出参数:[1, 3, 4]
*  复杂性分析:时间复杂度:O(),空间复杂度:O()
*  题目来源  : https://leetcode.com/problems/binary-tree-right-side-view/description/
*  日期:2018-07-23-08.46
***************************************************/
class Solution4
{
public:
    vector<int> rightSideView(TreeNode* root)
    {
        vector<int> result;
        if(root==NULL) return result;
        queue<pair<TreeNode*,int> > Q;
        Q.push(make_pair(root,1));
        int level=0;
        while(!Q.empty())
        {
            TreeNode* node=Q.front().first;
            int deep=Q.front().second;
            Q.pop();
            if(deep!=level)
            {
                result.push_back(node->val);
                level=deep;
            }else
            {
                result[deep-1]=node->val;
            }
            if(node->left)
            {
                Q.push(make_pair(node->left,deep+1));
            }
            if(node->right)
            {
                Q.push(make_pair(node->right,deep+1));
            }
        }
        return result;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值