110. 平衡二叉树
已解答
简单
相关标签
相关企业
给定一个二叉树,判断它是否是
平衡二叉树
示例 1:
输入:root = [3,9,20,null,null,15,7] 输出:true示例 2:
输入:root = [1,2,2,3,3,null,null,4,4] 输出:false示例 3:
输入:root = [] 输出:true提示:
- 树中的节点数在范围
[0, 5000]
内-104 <= Node.val <= 104
题解:
/*** 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 {private int getHeight(TreeNode root){if(root == null){return 0;}int lHeight = getHeight(root.left);if(lHeight == -1){return -1;}int rHeight = getHeight(root.right);if(rHeight == -1){return -1;}if(lHeight - rHeight > 1 || rHeight - lHeight > 1){return -1;}return Math.max(lHeight,rHeight) + 1;}public boolean isBalanced(TreeNode root) {if(getHeight(root) != -1){return true;}else return false;}
}
257. 二叉树的所有路径
已解答
简单
相关标签
相关企业
给你一个二叉树的根节点
root
,按 任意顺序 ,返回所有从根节点到叶子节点的路径。叶子节点 是指没有子节点的节点。
示例 1:
输入:root = [1,2,3,null,5] 输出:["1->2->5","1->3"]示例 2:
输入:root = [1] 输出:["1"]提示:
- 树中节点的数目在范围
[1, 100]
内-100 <= Node.val <= 100
题解:
/*** 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 {private void traversal(TreeNode root, List<Integer> paths, List<String> res) {paths.add(root.val);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)).append("->");}sb.append(paths.get(paths.size() - 1));res.add(sb.toString());return ;}if(root.left != null){traversal(root.left,paths,res);paths.remove(paths.size() - 1);}if(root.right != null){traversal(root.right,paths,res);paths.remove(paths.size() - 1);}}public List<String> binaryTreePaths(TreeNode root) {List<String> res = new ArrayList<>();// 存最终的结果if (root == null) {return res;}List<Integer> paths = new ArrayList<>();// 作为结果中的路径traversal(root, paths, res);return res;}
}
404. 左叶子之和
已解答
简单
相关标签
相关企业
给定二叉树的根节点
root
,返回所有左叶子之和。示例 1:
输入: root = [3,9,20,null,null,15,7] 输出: 24 解释: 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24示例 2:
输入: root = [1] 输出: 0提示:
- 节点数在
[1, 1000]
范围内-1000 <= Node.val <= 1000
题解:
/*** 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;int leftValue = sumOfLeftLeaves(root.left);int rightValue = sumOfLeftLeaves(root.right);int midValue = 0;if(root.left != null && root.left.left == null && root.left.right == null){midValue = root.left.val;}int sum = midValue + leftValue + rightValue;return sum;}
}