代码随想录算法训练Day15|110、平衡二叉树、257. 二叉树的所有路径、 404.左叶子之和、 222.完全二叉树的节点个数

110、平衡二叉树

        思路:求高度采用后续遍历。只要子树不平衡,就返回-1,不断向上传递。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean isBalanced(TreeNode root) {
        return maxDepth(root) != -1;

    }

    public int maxDepth(TreeNode cur){
        if(cur == null) return 0;
        int left = maxDepth(cur.left);
        int right = maxDepth(cur.right);
        if(left == -1 || right == -1){
            return -1;
        }
        if(Math.abs(left - right) > 1){
            return -1;
        }
        int max = Math.max(left, right) + 1;
        return max;
    }
}

257. 二叉树的所有路径

        

        思路:遍历方式要用前序,才能把父节点的信息传递给子节点。

        代码框架:先定义结果的二维数组。如果根节点为空直接返回。然后定义单挑结果路径,

        递归:确定终止条件。左孩子为空和又孩子为空时,是叶子节点。把路径字符串加到结果中。

        处理当前节点,把当前节点添加进path中

        递归左子树,当左孩子不为空时。递归完之后,删除最后一个节点路径作为回溯。

        递归右子树,当右孩子不为空时。同上

        

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<>();
        if(root == null) return res;
        List<Integer> path = new ArrayList<>();
        solute(root, res, path);
        return res;
    }

    public void solute(TreeNode root, List<String> res,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) + "->");
            }
            sb.append(path.get(path.size() - 1));
            res.add(sb.toString());
            return;
        }

        if(root.left != null){
            solute(root.left, res, path);
            path.remove(path.size() - 1);
        }

        if(root.right != null){
            solute(root.right, res, path);
            path.remove(path.size() - 1);
        }
    }
}

        

404.左叶子之和

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        if(root == null) return 0;
        if(root.right == null && root.left == null){
            return 0;
        }
        int mid = 0;
        int left = sumOfLeftLeaves(root.left);
        int right = sumOfLeftLeaves(root.right);
        if(root.left != null && root.left.left ==null && root.left.right == null){
            mid = root.left.val;
        }

        int sum = mid + left + right;
        return sum;
    }
}

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

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    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 != null){
            leftDepth++;
            left = left.left;

        }
        while(right !=null){
            rightDepth++;
            right = right.right;
        }
        if(leftDepth == rightDepth){
            return (2 << leftDepth) - 1;
        }
        return countNodes(root.left) + countNodes(root.right) + 1;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值