二刷代码随想录训练营Day 15|力扣110.平衡二叉树、257. 二叉树的所有路径、404.左叶子之和、222.完全二叉树的节点个数

1.平衡二叉树

题目链接/文章讲解/视频讲解:代码随想录

代码:递归 

class Solution {
public:
    int getHeight(TreeNode* node){
        if(node == NULL) return 0;
        // 左
        int left = getHeight(node->left);
        // 右
        int right = getHeight(node->right);
        // 中
        if(left == -1 || right == -1){
            return -1;
        }
        if(left - right > 1 || right - left > 1){
            return -1;
        }
        return max(left,right) + 1;
    }
    bool isBalanced(TreeNode* root) {
        int result = getHeight(root);
        if(result == -1){
            return false;
        }
        return true;
    }
};

 note:我一开始还在想,怎么把这个高度和判断结果都返回去,或者把高度用全局变量或参数来传递。后来才知道——可以用返回值高度为-1来表示false!!!

如果本题要使用迭代法,可以用这样的思路:用一个函数来求传入节点的高度(其实是根结点的最大深度)。在主函数里,用后序遍历去判断每个节点的左右孩子的高度差有没有超过1。

2.二叉树的所有路径

题目链接/文章讲解/视频讲解:代码随想录

代码:递归法 

/**
 * 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 traversal(TreeNode* node,vector<int>& path,vector<string>& result){
        // 中
        path.push_back(node->val); // 最后一个节点也要放进path里
        // 递归出口
        if(node->left == NULL && node->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(node->left){
            traversal(node->left,path,result);
            path.pop_back();
        }
        // 右
        if(node->right){
            traversal(node->right,path,result);
            path.pop_back();
        }
    }
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> result;
        vector<int> path;
        if(root == NULL) return result;
        traversal(root,path,result);
        return result;
    }
};

 note:这种要遍历整颗树的情况,返回值是void。要处理叶子节点时,终止条件就改为是叶子节点时。同时,为了让叶子节点也被处理,中间的处理逻辑写道终止条件的上面。以及把判断节点是否为空的代码,加到了调用递归前。

代码:迭代法 

/**
 * 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:
    vector<string> binaryTreePaths(TreeNode* root) {
        stack<TreeNode*> treeSt;
        stack<string> pathSt;
        vector<string> result;
        if(root == NULL) return result;
        treeSt.push(root);
        pathSt.push(to_string(root->val));
        while(!treeSt.empty()){
            TreeNode* node = treeSt.top();
            treeSt.pop();
            string path = pathSt.top();
            pathSt.pop();
            if(node->left == NULL && node->right == NULL){
                result.push_back(path);
            }
            if(node->right){
                treeSt.push(node->right);
                pathSt.push(path + "->" + to_string(node->right->val));
            }
            if(node->left){
                treeSt.push(node->left);
                pathSt.push(path + "->" + to_string(node->left->val));
            }
        }
        return result;
    }
};

note:用了另一个栈来保存路径。 

3.左叶子之和

题目链接/文章讲解/视频讲解:代码随想录

代码:递归法 

class Solution {
public:
    int sumOfLeftLeaves(TreeNode* root) {
       if(root == 0) return 0;
       // 左
       int leftValue = sumOfLeftLeaves(root->left);
       if(root->left != NULL && root->left->left == NULL && root->left->right == NULL){
        leftValue = root->left->val;
       }
       // 右
       int rightValue = sumOfLeftLeaves(root->right);
       // 中
       int value = leftValue + rightValue;
       return value;
    }
};

 note:递归法用后序遍历,因为要通过递归函数的返回值来累加求取左叶子数值之和

也可以用其它遍历顺序,只不过要加个全局变量。 

代码:迭代法 

class Solution {
public:
    int sumOfLeftLeaves(TreeNode* root) {
        stack<TreeNode*> st;
        int sum = 0;
        if(root == NULL) return sum;
        st.push(root);
        while(!st.empty()){
            TreeNode* node = st.top();
            if(node != NULL){
                st.pop();
                // 右
                if(node->right) st.push(node->right);
                // 左
                if(node->left) st.push(node->left);
                // 中
                st.push(node);
                st.push(NULL);
            }else{
                st.pop();
                node = st.top();
                st.pop();
                if(node->left != NULL && node->left->left == NULL && node->left->right == NULL){
                    sum += node->left->val;
                }
            }
        }
        return sum;
    }
};

 note:其实就是遍历所有的节点,找到符合左孩子不为空,且左孩子为叶子节点的节点,然后累加它们左孩子的值返回就好了。

4.完全二叉树的节点个数

题目链接/文章讲解/视频讲解:代码随想录

代码: 

class Solution {
public:
    int countNodes(TreeNode* root) {
        // 递归出口
        if(root == nullptr) return 0;
        TreeNode* left = root->left;
        TreeNode* right = root->right;
        int leftDepth = 0;
        int rightDepth = 0;
        while(left){
            left = left->left;
            leftDepth++;
        }
        while(right){
            right = right->right;
            rightDepth++;
        }
        if(leftDepth == rightDepth){
            return (2 << leftDepth) - 1;  // 注意(2<<1) 相当于2^2,所以leftDepth初始为0
        }
        // 后序遍历
        return countNodes(root->left) + countNodes(root->right) + 1;
    }
};

 note:这里的递归出口是判断子树为满二叉树。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值