代码随想录算法训练营第15天

递归只要关心当前这个结点root要做什么,什么时候做就行,不要进入到递归的细节中

116. 填充每个节点的下一个右侧节点指针 - 力扣(LeetCode)

填充每个节点的next指针,如果递归的参数只有一个root,那么只能连接当前root的左右结点,跨父结点就做不到了,因此传参数时要传两个,这样才能把跨父结点的结点连起来。

class Solution {
    public Node connect(Node root) {
        if (root == null) return null;
        connectNode(root.left, root.right);
        return root;
    }

    public void connectNode(Node root1, Node root2) {
        if (root1 == null || root2 == null) return;
        root1.next = root2;

        connectNode(root1.left, root1.right);
        connectNode(root2.left, root2.right);
        connectNode(root1.right, root2.left);
    }
}

114. 二叉树展开为链表 - 力扣(LeetCode)

这道题典型的,不要想递归的细节,只要想好最后一层递归结束后数据是什么样子,当前root结点需要在哪里处理这些递归结束的数据,需要怎么处理。

二叉树展开为列表,当前root需要在递归结束后,root的左子树和右子树都被展开成了链表后,将root的右子树接在左子树后面,然后将左子树移到root的右子树的位置,就得到了最后的结果。

class Solution {
    public void flatten(TreeNode root) {
        if (root == null) return;
        flatten(root.left);
        flatten(root.right);

        //后序位置处理
        TreeNode left = root.left;
        TreeNode right = root.right;

        root.left = null;
        root.right = left;

        TreeNode p = root;
        while (p.right != null) {
            p = p.right;
        }
        p.right = right;
    }
}

这里借用labuladong的一段话:

110. 平衡二叉树 - 力扣(LeetCode)

平衡二叉树是指左右子树的深度差不超过1。

对于每一个结点求最大高度(左右子树高度的最大值),在这个过程中,如果左右子树的高度差超过了1,就说明不是平衡二叉树,返回-1;

class Solution {
    public boolean isBalanced(TreeNode root) {
        return getHeight(root) != -1;

    }

    public int getHeight(TreeNode root) {
        if (root == null) return 0;
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        if (leftHeight == -1 || rightHeight == -1 ||Math.abs(leftHeight - rightHeight) > 1) return -1;
        return Math.max(leftHeight, rightHeight) + 1;

    }

}

257. 二叉树的所有路径 - 力扣(LeetCode)

递归+回溯

回溯体现在遍历完左子树和右子树后要回退。

class Solution {
    List<String> res = new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        if (root == null) return null;
        List<Integer> path = new ArrayList<>();
        traverse(root, path);
        return res;
    }

    public void traverse(TreeNode root, List<Integer> path) {
        path.add(root.val);
        if (root.left == null && root.right == null) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < path.size() - 1; i++) {
                sb.append(path.get(i)).append("->");
            }
            sb.append(path.get(path.size() - 1));
            res.add(sb.toString());
            return;
        }
        if (root.left != null) {
            traverse(root.left, path);
            path.remove(path.size() - 1);
        }
        if (root.right != null) {
            traverse(root.right, path);
            path.remove(path.size() - 1);
        }
    }
}

404. 左叶子之和

class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        if (root == null) return 0;
        int sum = 0;
        if (root.left != null && root.left.left == null && root.left.right == null) {
            sum += root.left.val;
        }
        sum += sumOfLeftLeaves(root.left);
        sum += sumOfLeftLeaves(root.right);
        return sum;
    }
}

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

class Solution {
    public int countNodes(TreeNode root) {
        if (root == null) return 0;
        int sum = 1;
        if (root.left != null) sum += countNodes(root.left);
        if (root.right != null) sum += countNodes(root.right);
        return sum;
    }
}

day15 终于做完了,上周做了一周的中期报告,这周mt给安排了个任务,明天去改完需求,接下来的时间想一想文章。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值