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;
}
}