Leetcode二叉树

文章介绍了如何解决几种二叉树问题,包括判断是否为平衡二叉树、获取二叉树路径、求解左叶子节点之和以及找到最底部的左叶子节点。使用的方法包括后序遍历检测平衡,前序遍历获取路径,递归或层序遍历处理节点和路径。

110. Balanced Binary Tree

因为求子叶,所以用后序遍历

class Solution {
public:
    int getHeight(TreeNode* root){
        if(root == NULL) return 0;
        int res;

        int leftHeight = getHeight(root->left);
        if(leftHeight == -1) return-1;
        int rightHeight = getHeight(root->right);
        if(rightHeight == -1) return -1;
        if(abs(leftHeight-rightHeight) > 1){
            res = -1;
        }else{
            res = max(leftHeight, rightHeight) + 1;
        }
        return res;
        
    }
    bool isBalanced(TreeNode* root) {
        
        return getHeight(root) == -1? false:true;
        
    }
};

第一个function在左右两边的高度差超过1时就已经返回-1证明为假

 257. Binary Tree Paths

前序(回溯)

class Solution {
public:
    void traversal(TreeNode* curr, vector<int>& path, vector<string>& res){
        path.push_back(curr->val);
        if(curr->left == NULL && curr->right == NULL){
            string sPath;
            for(int i=0; i<path.size()-1; i++){
                sPath = sPath + to_string(path[i]) + "->";
            }
            sPath += to_string(path[path.size()-1]);
            res.push_back(sPath);
            return;
        }

        if(curr->left){
            traversal(curr->left, path, res);
            path.pop_back();
        }if(curr->right){
            traversal(curr->right, path, res);
            path.pop_back();
        }
    }
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> res;
        vector<int> path;
        traversal(root, path, res);
        return res;
        
    }
};

用前序:因为路径记录自上而下,需要保存的是上面的数据,这样一条路径结束后,可以把下面的数据删掉记录另一条路径

迭代:

递归函数参数和返回值(一条路径结束以后,我们需要记录节点和路径)

确认终止条件(一条路径结束了之后要做什么:记录一整个路径,返回上一个节点) 

确定单次逻辑(需要将节点的值记录进path中并完成回溯)【我感觉一般情况下回溯的部分是要写在最后的,不是说要在字面上写在最后,而是一整个过程结束之后才用到回溯】这里的回溯是要把path中结束的节点删掉

404. Sum of Left Leaves

class Solution {
public:
int sum = 0;
    int sumOfLeftLeaves(TreeNode* root) {
        if(root == NULL) return 0;
        if(root->left != NULL && root->left->left == NULL && root->left->right == NULL){
            sum += root->left->val;
        }
        sumOfLeftLeaves(root->left);
        sumOfLeftLeaves(root->right);

        return sum;
    }
};
class Solution {
public:
    int sumOfLeftLeaves(TreeNode* root) {
        if(root == NULL) return 0;
        int leftVal = sumOfLeftLeaves(root->left);
        if(root->left != NULL && root->left->left == NULL && root->left->right == NULL){
            leftVal += root->left->val;
        }
        
        int rightVal = sumOfLeftLeaves(root->right);
        int sum = leftVal + rightVal;
        return sum;
    }
};

513. Find Bottom Left Tree Value

这道题要注意bottom leaf是指最大深度的左子叶

思路:找最大深度,记录最大深度的左子叶

1.层序

class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        queue<TreeNode*> que;
        int res = 0;
        que.push(root);
        while(!que.empty()){
            int size = que.size();

            for(int i=0; i<size; i++){
                TreeNode* curr = que.front();
                que.pop();
                if(i == 0) res = curr->val;
                if(curr->left) que.push(curr->left);
                if(curr->right) que.push(curr->right);
            }
        }

        return res;
    }
};

这里只需要记录每一层的第一个值就是最左的值,最后记录的就是最大深度的左子叶

2.递归

class Solution {
public:
    int maxDepth = INT_MIN;
    int res;
    void traversal(TreeNode* root, int depth){
        if(root->left == NULL && root->right == NULL){
            if(depth > maxDepth){
                res = root->val;
                maxDepth = depth;
            }
            return;
        }
        if(root->left){
            traversal(root->left, depth+1);
        }
        if(root->right){
            traversal(root->right, depth+1);
        }
        return;
    }
    int findBottomLeftValue(TreeNode* root) {
        traversal(root, 0);
        return res;
    }
};

这里需要注意的一点

还可以写成

/* depth++;

* traversal(root->left, depth);

* depth--;

*/

当写成第一种方法的时候,隐藏着回溯(返回上一级自动就变成之前的depth了,不需要再-1了)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值