leecode 练习--树

1 篇文章 0 订阅
1 篇文章 0 订阅
  1. Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.

Note: The length of path between two nodes is represented by the number of edges between them.

求树的深度height时,分别遍历左右子树,得到左右子树深度lh和rh,返回左右子树最大深度+1,但同时要跟现已知最大diameter比较,及时更新diameter: diameter为当前节点左右深度之和。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int diameterOfBinaryTree(TreeNode* root) {
        int dia = 0;
        height(root,dia);
        return dia;
    }
    int height(TreeNode* node, int &diame)
    {
        if(node == NULL)
            return 0;
        int lh = 0,rh = 0;
        lh = height(node->left,diame);
        rh = height(node->right,diame);
        int span = lh + rh;
        diame = max(span,diame);
        return 1 + max(lh,rh);

    }
};
  1. Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.
    看清题目是二叉搜索树,因此用中序遍历,传值sum作为一个累积量要不断更新,用sum更新节点值。
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* convertBST(TreeNode* root) {
        int sum = 0;
        inorder(root,sum);
        return root;


    }
    void inorder(TreeNode* node, int &sum)   //中序遍历二叉搜索树
    {
        if(!node)
            return;
        inorder(node->right,sum);
        int tempsum = node->val;
        sum += tempsum;
        node->val = sum;
        inorder(node->left,sum);

    }

};

/***** 广度优先 BFS queue*****************/

/**Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.

The distance between two adjacent cells is 1.*/

class Solution {
public:
    vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
        //initilize
        int m = matrix.size();
        int n = matrix[0].size();
        int infinity = INT_MAX/2;
        vector<vector<int>> newmatrix(m,vector<int>(n,infinity));
        queue<pair<int,int>*> que;
        int i,j;
        for(i = 0;i < m;i++)
            for(j = 0;j < n;j++)
            {
                if(!matrix[i][j])
                {
                    newmatrix[i][j] = 0;
                    que.push(new pair<int,int>(i,j));
                }
            }
    //        que.push(nullptr);
    //            int dist  = 0;        
        while(!que.empty())
        {
            pair<int,int>* p = que.front();
            que.pop();
       //     if(p == nullptr)
     //       {
     //             dist++;          //记录BFS层数
     //            if(!que.empty())
    //                  que.push(nullptr);
   //              continue;
   //         }
            i = p->first;
            j = p->second;

            if(i>0)
            {
                if(newmatrix[i-1][j] == infinity)
                {
                    newmatrix[i-1][j] = min(newmatrix[i-1][j],newmatrix[i][j]+1);
                    que.push(new pair<int,int>(i-1,j));
                }
            }
            if(i<m-1)
            {
                if(newmatrix[i+1][j] == infinity)
                {
                    newmatrix[i+1][j] = min(newmatrix[i+1][j],newmatrix[i][j]+1);
                    que.push(new pair<int,int>(i+1,j));
                }
            }
             if(j>0)
            {
                if(newmatrix[i][j-1] == infinity)
                {
                    newmatrix[i][j-1] = min(newmatrix[i][j-1],newmatrix[i][j]+1);
                    que.push(new pair<int,int>(i,j-1));
                }
            }
            if(j<n-1)
            {
                if(newmatrix[i][j+1] == infinity)
                {
                    newmatrix[i][j+1] = min(newmatrix[i][j+1],newmatrix[i][j]+1);
                    que.push(new pair<int,int>(i,j+1));
                }
            }


            delete p;

        }

        return newmatrix;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值