【代码随想录|二叉树递归法02】

                                       二叉树递归三道题

110.平衡二叉树

题目链接https://leetcode.cn/problems/balanced-binary-tree/description/

这里用的是后序遍历,就是根结点问左节点和右节点要信息,要的信息要先列出来,只要后面传给左、右节点的是-1就就接着往上返回-1。

1、明确递归函数的参数和返回值

参数就是传入树的根节点,返回就返回这棵树的深度,所以返回值为int类型。

2、确定终止条件

根节点为空的时候,就返回值为0,一直遍历到左节点为空的时候就返回值

3、确定单层递归的逻辑

(1)左子树已经不满足二叉树了,返回-1

(2)右子树已经不满足二叉树了,返回-1

(3)左右子树都满足二叉树,但是左子树右子树的绝对值大于1,返回-1

(4)左右子树都满足二叉树,左右子树绝对值小于等于1,返回左右较大子树的高度还加上自己本身1个单位高度

int getheight(TreeNode* root) {//错误代码
    if (root == NULL)
        return 0;
    int leftdepth = getheight(root->left);
    int rightdepth = getheight(root->right);
    if (leftdepth == -1)
        return -1;
    if (rightdepth == -1)
        return -1;
    return max(leftdepth, rightdepth) + 1;
}

最开始我递归函数这里报错,因为这里漏了第三个条件,当左右子树高度差绝对值大于1的时候要返回-1 

//正确代码
int getheight(TreeNode* root) {//接收不能用引用,是引用就无法写终止条件root == NULL
    if (root == NULL)
        return 0;
    int leftdepth = getheight(root->left);
    int rightdepth = getheight(root->right);
    if (leftdepth == -1)//左
        return -1;
    if (rightdepth == -1)//右
        return -1;
    if (abs(leftdepth - rightdepth) > 1)
        return -1;
    return max(leftdepth, rightdepth) + 1;//根
}
class Solution {
public:
    bool isBalanced(TreeNode* root) {
        return getheight(root) == -1 ? false : true;
    }
};

257.二叉树的所有路径

题目链接https://leetcode.cn/problems/binary-tree-paths/description/

  1. 递归函数参数以及返回值根节点。

记录路径的path和存放结果的res

     2.确定递归终止条件

应该是进行前序遍历,

 if(root->left==NULL&&root->right==NULL)

当左右孩子都是空的时候就找到叶子结点

      3.确定单层递归逻辑

在遍历的时候,每到一个节点就先加入到path数组里面,然后进行递归左子树,在遍历到根节点加入到res里面之后就会一个一个pop直到root结点,然后再进行递归右子树,在遍历到根节点加入到res里面之后也会一个一个pop直到root结点。

/**
 * 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 {
    void travel(TreeNode* root,vector<int>& path,vector<string>& res){
        path.push_back(root->val);//要放前面判断根节点前面不然最后一个节点就不能加入到path里面
        if(root->left==NULL&&root->right==NULL){
            string spath;
            int i;
            for(i=0;i<path.size()-1;i++){
                spath+=to_string(path[i]);
                spath+="->";
            }
            spath+=to_string(path[i]);
            res.push_back(spath);
            return;
        }
        if(root->left) {travel(root->left,path,res);path.pop_back();}
        if(root->right) {travel(root->right,path,res);path.pop_back();}
    }
public:
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<int> path;
        vector<string> res;
        if(root==NULL)return res;
        else travel(root,path,res);
        return res;
    }
};

404.左叶子之和

题目链接https://leetcode.cn/problems/sum-of-left-leaves/description/

1.确定递归函数的参数和返回值

​​​​​返回值是该结点的左右结点的值之和,所以参数是int

2.确定终止条件

中节点向左右子树要他们保存自己左右节点的和,那当该结点指向的左节点和右节点已经为0了,那么该结点直接返回0

当该结点为空时,那肯定没有左右结点了,直接返回0

上面两个任意一个当终止条件都行

3、确定单层递归的逻辑

遍历左叶子节点和右叶子节点

遇到左叶子节点就保存数值,右叶子节点不用管,到时候递归到叶子节点他就自己返回值0了

class Solution {
public:
    int sumOfLeftLeaves(TreeNode* root) {
        if (root == NULL)
            return 0;
        if (root->left == NULL && root->right == NULL) {
            return 0;
        }//不写也没错,因为只是会少递归一次根节点的值
        int left = sumOfLeftLeaves(root->left);//左
        if (root->left && root->left->left == NULL &&
            root->left->right == NULL) {//右
            left = root->left->val;
        }
        int right = sumOfLeftLeaves(root->right);//根
        int sum = left + right;
        return sum;
    }
};

222.完全二叉树的结点个数

题目链接https://leetcode.cn/problems/count-complete-tree-nodes/description/

用的是后序遍历,直接写后面三步也能通过,这么写时间复杂度会低一点

注意这里

所以要返回正确的值的话(2的深度次方-1),可以把临时变量先指向root->left,让他们从0开始计数,这样在计算在计算2向左移的时候才不会错


class Solution {//时间复杂度是log(n)*log(n)
public:
    int countNodes(TreeNode* root) {
          if (root == NULL) return 0;
        TreeNode* left = root->left;
        TreeNode* right = root->right;
        int leftDepth = 0, rightDepth = 0; 
        while (left) {  
            left = left->left;
            leftDepth++;
        }
        while (right) { 
            right = right->right;
            rightDepth++;
        }
        if (leftDepth == rightDepth) {
            return (2 << leftDepth) - 1; }//求的是2的leftdepth+1次方-1;
        int leftnode = countNodes(root->left);
        int rightnode = countNodes(root->right);
        return leftnode + rightnode + 1;
    }
};

在写的时候直接在while循环里面写while(root->left)root->left=root->left->left,就没通过,好家伙,这么写直接把人二叉树的左节点 都改了(hhh),所以要用临时变量TreeNode* left先指向一个树的结点。

//错误做法
       while(root->left){
            root->left=root->left->left;
            leftdepth++;
        }
        while(root->right){
           root->right=root->right->right;
            rightdepth++;
        }
       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值