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

当日任务

 110.平衡二叉树 
 257. 二叉树的所有路径
 404.左叶子之和
 222.完全二叉树的节点个数

110.平衡二叉树 

题目链接 代码随想录

/**
 * 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 getHeight(root) == -1 ? false : true;
    }

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

257. 二叉树的所有路径

题目链接 . - 力扣(LeetCode)

/**
 * 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) {
        Stack<Object> stack = new Stack<Object>();
        List<String> result = new ArrayList<>();
        if (root == null) {return result;}
        stack.push(root);
        stack.push(root.val + "");

        while (!stack.isEmpty()) {
            String tmpStr = (String)stack.pop();
            TreeNode tmpNode = (TreeNode)stack.pop();
            if (tmpNode.left == null && tmpNode.right == null) {
                result.add(tmpStr);
            }

            if (tmpNode.right != null) {
                stack.push(tmpNode.right);
                stack.push(tmpStr + "->" + tmpNode.right.val);
            }

            if (tmpNode.left != null) {
                stack.push(tmpNode.left);
                stack.push(tmpStr + "->" + tmpNode.left.val);
            }
        }
        return result;
    }

}



/**
 * 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<TreeNode> paths = new ArrayList<TreeNode>();
        List<String> result = new ArrayList<String>();
        if (root == null) {return result;}
        inOrdertravel(root, paths, result);
        return result;
    }
    void inOrdertravel(TreeNode root, List<TreeNode> paths, List<String> result){
        paths.add(root); //中
        if (root.left == null && root.right == null) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < paths.size() - 1; i++) {
                sb.append(paths.get(i).val).append("->");
            }
            sb.append(paths.get(paths.size() - 1).val);
            result.add(sb.toString());
            return;
        }

        if (root.right != null) {   //右
            inOrdertravel(root.right, paths, result);
            paths.remove(paths.size() - 1);  //回溯
        }

        if (root.left != null) {   //左
            inOrdertravel(root.left, paths, result);
            paths.remove(paths.size() - 1);  //回溯
        }
    }

}


404.左叶子之和

题目链接 . - 力扣(LeetCode)

/**
 * 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 res = 0;
    public int sumOfLeftLeaves(TreeNode root) {
         if (root == null) return res;
        getLeftVal(root, false);
        return res;
    }

    public void getLeftVal(TreeNode root, Boolean ifLeft){
        if (root.left == null && root.right == null && ifLeft == true) {
            res += root.val;
        }
        if (root.left != null) {
            getLeftVal(root.left, true);
        }
        if (root.right != null) {
            getLeftVal(root.right, false);
        }
        return;
    }

}


/**
 * 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) {
        Stack<Object> stack = new Stack<>();
        int res = 0;
        if (root == null) {return res;}
        stack.push(Boolean.FALSE);
        stack.push(root);

        while (!stack.isEmpty()) {
            TreeNode tmpNode = (TreeNode)stack.pop();
            boolean ifLeft = (Boolean)stack.pop();
            if (ifLeft == Boolean.TRUE && tmpNode.left == null && tmpNode.right == null) {
                res += tmpNode.val;
            }

            if (tmpNode.right != null) {
                stack.push(Boolean.FALSE);
                stack.push(tmpNode.right);
            }

            if (tmpNode.left != null) {
                stack.push(Boolean.TRUE);
                stack.push(tmpNode.left);
            }
        }
        return res;
    }
}

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

题目链接   . - 力扣(LeetCode)

/**
 * 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) {
        Stack<TreeNode> stack = new Stack<TreeNode>();
        int len = 0;
        if (root == null) {return len;}
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode tmp = stack.peek();
            if (tmp != null) {
                stack.pop();
                if (tmp.right != null){stack.push(tmp.right);}
                if (tmp.left != null){stack.push(tmp.left);}
                stack.push(tmp);
                stack.push(null);
            } else {
                stack.pop();
                stack.pop();
                len++;
            }
        }
        return len;
    }


}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值