代码随想录第17天|110.平衡二叉树, 257. 二叉树的所有路径, 404.左叶子之和

LeetCode110.平衡二叉树

题目链接:110. 平衡二叉树 - 力扣(LeetCode)

思路:

比较高度必然要后序遍历

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int getdepth(TreeNode* root) {
        //返回平衡二叉树的值,如果不是则返回-1
        if(root == NULL) return 0;
        int leftnum = getdepth(root->left);
        if(leftnum == -1) return -1;
        int rightnum = getdepth(root->right);
        if(rightnum == -1) return -1;
        if(abs(leftnum - rightnum) <= 1) return 1 + max(leftnum,rightnum);
        else return -1;
    }

    bool isBalanced(TreeNode* root) {
        if(getdepth(root) == -1) return false;
        return true;
    }
};

LeetCode257. 二叉树的所有路径

题目链接:257. 二叉树的所有路径 - 力扣(LeetCode)

思路:

题目要求从根节点到叶子的路径,所以需要前序遍历,这样才方便让父节点指向孩子节点,找到对应的路径。

在做二叉树递归和回溯时,回溯要和递归永远在一起。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    void travel(TreeNode* cur, vector<int>& path, vector<string>& result) {
        path.push_back(cur->val);//中,中写在这里的原因是最后一个节点也要加在path
        //叶子节点处理
        if(cur->left == NULL && cur->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]);
            result.push_back(spath);
            return;
        }
        if(cur->left) {//左
            travel(cur->left,path,result);
            path.pop_back();//回溯
        }
        if(cur->right) {//右
            travel(cur->right,path,result);
            path.pop_back();//回溯
        }
    }
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> result;
        vector<int> path;
        if(root == NULL) return result;
        travel(root,path,result);
        return result;
    }
};

LeetCode404.左叶子之和

题目链接:404. 左叶子之和 - 力扣(LeetCode)

思路:

判断题意:左叶子的明确定义:节点A的左孩子不为空,且左孩子的左右孩子都为空(说明是叶子节点),那么A节点的左孩子为左叶子节点。

判断当前节点是不是左叶子是无法判断的,必须要通过节点的父节点来判断其左孩子是不是左叶子。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int leftsval(TreeNode* root) {
        if(root == NULL) return 0;
        if(root->left == NULL && root->right == NULL) return 0;
        int leftval = leftsval(root->left);
        int rightval = leftsval(root->right);
        //判断左节点是否为左叶子节点
        if(root->left && !root->left->left && !root->left->right) {
            leftval = root->left->val;
        }   
        int sum = leftval + rightval;
        return sum;
    }

    int sumOfLeftLeaves(TreeNode* root) {
        return leftsval(root);
    }
};

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值