代码随想录【Day 17】 | 110.平衡二叉树 (优先掌握递归)、 257. 二叉树的所有路径 (优先掌握递归)、 404.左叶子之和 (优先掌握递归)

● 110.平衡二叉树

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

卡尔文解

题外话

咋眼一看这道题目和104.二叉树的最大深度 (opens new window)很像,其实有很大区别。

这里强调一波概念:

  • 二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数
  • 二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数。

但leetcode中强调的深度和高度很明显是按照节点来计算的,如图:
在这里插入图片描述

关于根节点的深度究竟是1 还是 0,不同的地方有不一样的标准,leetcode的题目中都是以节点为一度,即根节点深度是1。但维基百科上定义用边为一度,即根节点的深度是0,我们暂时以leetcode为准(毕竟要在这上面刷题)。

因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中)

有的同学一定疑惑,为什么104.二叉树的最大深度 (opens new window)中求的是二叉树的最大深度,也用的是后序遍历。

那是因为代码的逻辑其实是求的根节点的高度,而根节点的高度就是这棵树的最大深度,所以才可以使用后序遍历。

解题思路重点:

代码实现:

/**
 * 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 {

private:

    int getHeight( TreeNode *node ){

        if ( !node ) return 0;

        int leftHeight = getHeight( node->left );
        if ( leftHeight == -1 ) return -1;

        int rightHeight = getHeight( node->right );
        if ( rightHeight == -1 ) return -1;

        int result = abs( leftHeight - rightHeight );

        if (result > 1 ) return -1;
        else return( 1 + max( leftHeight, rightHeight) );

    }
public:
    bool isBalanced(TreeNode* root) {

        return( getHeight( root ) != -1 );

    }
};

257. 二叉树的所有路径 (优先掌握递归)

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

卡尔文解

代码实现:

/**
 * 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 {

private: 
    
    void traversal( TreeNode *node, vector<int> &path, vector<string> &result ){

        path.push_back( node->val );

        if ( !node->left && !node->right ){ // Hit leaf node
            
            int size = path.size();
            string tmp;
            for ( int i = 0; i < (size - 1); i++ ){
                tmp += to_string( path[ i ] );
                tmp += "->";
            }

            tmp += to_string( path[ size - 1 ] );
            result.push_back( tmp );
            return;
        }

        if ( node->left ){
            traversal( node->left, path, result );
            path.pop_back(); // Back track
        }

        if ( node->right ){
            traversal( node->right, path, result );
            path.pop_back(); // Back track
        }



    }
public:
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<int> path;
        vector<string> result;

        if ( !root ) return result;

        traversal( root, path, result );
        return result;

    }
};

404.左叶子之和 (优先掌握递归)

题目链接:404.左叶子之和 (优先掌握递归)

卡尔文解

解题思路重点:

  1. 后序遍历

代码实现:

/**
 * 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 sumOfLeftLeaves(TreeNode* root) {
        if (root == NULL) return 0;
        if (root->left == NULL && root->right== NULL) return 0;

        int leftValue = sumOfLeftLeaves(root->left);    // 左
        int rightValue = sumOfLeftLeaves(root->right);  // 右

        if (root->left && !root->left->left && !root->left->right) { // 左子树就是一个左叶子的情况
            leftValue = root->left->val;
        }

        int sum = leftValue + rightValue;               // 中
        return sum;
    }
};

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值