代码随想录算法训练营第十七天|LeetCode 110. 平衡二叉树、LeetCode 257. 二叉树的所有路径 、LeetCode 404.左叶子之和。

LeetCode 110. 平衡二叉树

题目链接:LeetCode 110. 平衡二叉树

后序遍历:

class Solution {
public:
    int getHeight(TreeNode* root)
    {
        if(root == NULL)
        return 0;
        int leftheight = getHeight(root -> left);
        if(leftheight == -1)
        return -1;
        int rightheight = getHeight(root -> right);
        if(rightheight == -1)
        return -1;
        if(fabs(rightheight - leftheight) > 1)
        return -1;
        else
        {
            int height = 1 + max(rightheight,leftheight);
            return height;    
        }
    }
    bool isBalanced(TreeNode* root) {
        return getHeight(root) == -1 ? false : true;
    }
};

思路:通过后序遍历函数来计算左右子树高度,之后计算高度差;如果高度差大于1,返回-1否则返回计算高度(左右子树高度最大值 + 父节点高度)。最后判断函数返回值是否为-1便可判断出是否是平衡二叉树


LeetCode 257. 二叉树的所有路径

题目链接:LeetCode 257. 二叉树的所有路径

前序遍历:

class Solution {
private:
    void traversal(TreeNode *root, vector<int> &path,vector<string> &res)
    {
        path.push_back(root -> val);
        if(root -> left == NULL && root -> right == NULL)
        {
            string spath;
            for(int i = 0;i < path.size() - 1;i++)
            {
                spath += to_string(path[i]);
                spath += "->";
            }
            spath += to_string(path[path.size() - 1]);
            //将int类型转化为string类型
            res.push_back(spath);
        }
        if(root -> left)
        {
            traversal(root -> left,path,res);
            path.pop_back();//回溯
        }
        if(root -> right)
        {
            traversal(root -> right,path,res);
            path.pop_back();//回溯
        }
    }
public:
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> res;
        vector<int> path;
        if(root == NULL)
        return res;
        traversal(root,path,res);
        return res;
    }
};

思路:因寻找路径的过程是由父节点指向子节点,故采用前序遍历。在遍历时,先记录节点值,再转化为对应格式即可。而在遍历中,向左遍历和向右遍历时需要进行回溯,即pop_back,才能进行后续遍历。

小结:因遍历的终止条件为左右叶子节点均为空,故在函数中因先记录最后一个节点,即将(path.push_back(root -> val);)前置,否则就会缺少最后一个节点。

精简代码:

class Solution {
private:

    void traversal(TreeNode* root, string path, vector<string>& res) {
        path += to_string(root->val); // 中
        if (root->left == NULL && root->right == NULL) {
            res.push_back(path);
            return;
        }
        if (root->left) traversal(root->left, path + "->", res); // 左
        if (root->right) traversal(root->right, path + "->", res); // 右
    }

public:
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> res;
        string path;
        if (root == NULL) return res;
        traversal(root, path, res);
        return res;

    }
};

LeetCode 404.左叶子之和

题目链接:LeetCode 404. 左叶子之和

后序遍历:

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

思路:通过后序遍历,记录左子树与右子树中的左叶子节点,最后返回他们的和即可。

小结:在判断节点是否为左叶子节点时,要通过其父节点来判断;若父节点的左子节点不为空,且该节点的左右子节点均为空,则该节点为左叶子节点

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值