110.平衡二叉树
思路
后序遍历,计算到该节点的最大高度,判断左右子树相减是否大于1,接收子树的判断结果,如果false直接返回不用做判断了
代码
/**
* 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) {
int result=charge(root);
if(result==-1) return false;
return true;
}
public int charge(TreeNode root){
if(root==null) return 0;
int leftdepth=charge(root.left);
if(leftdepth==-1) return -1;
int rightdepth=charge(root.right);
if(rightdepth==-1) return -1;
if(Math.abs(leftdepth-rightdepth)>1) return -1;
return Math.max(leftdepth,rightdepth)+1;
}
}
257. 二叉树的所有路径
思路
前序遍历,利用List存储路径(方便回退),进来就存路径里,我们找到的路径不包含空节点,所以要对终止条件做限制。当到达终止条件时,将路径存到结果集里。
代码
/**
* 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> result=new ArrayList<>();
if(root==null) return result;
List<Integer> path=new ArrayList<>();
find(root,path,result);
return result;
}
public void find(TreeNode node,List<Integer> path,List<String> result){
path.add(node.val);
if(node.left==null&&node.right==null){
String s="";
for(int i=0;i<path.size()-1;i++){
s+=path.get(i)+"->";
}
s+=path.get(path.size()-1); //i在离开循环后就被回收了
result.add(s);
}
if(node.left!=null){
find(node.left,path,result);
path.remove(path.size()-1);
}
if(node.right!=null){
find(node.right,path,result);
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) {
int sum=0;
return find(root,0);
}
public int find(TreeNode node,int sum){
if(node==null) return 0;
int sum1=find(node.left,sum);
int sum2=find(node.right,sum);
if(node.left!=null&&node.left.left==null&&node.left.right==null) sum+=node.left.val;
return sum+=sum1+sum2;
}
}
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;
int count1=countNodes(root.left);
int count2=countNodes(root.right);
return count1+count2+1;
}
}