求根节点到叶节点数字之和
class Solution {
static int sum;
public int sumNumbers(TreeNode root) {
sum=0;
Sum(0,root);
return sum;
}
public static void Sum(int val,TreeNode root)
{
if(root==null) return;
int k=(val*10+root.val);
if(root.left==null&&root.right==null)
{
sum=sum+k;
}
Sum(k,root.left);
Sum(k,root.right);
}
}
平衡二叉树
class Solution {
public boolean isBalanced(TreeNode root) {
if(root==null) return true;
if(cal(root.left)>=100000||cal(root.right)>=100000) return false;
else return Math.abs(cal(root.left)-cal(root.right))<=1;
}
public int cal(TreeNode root)
{
if(root==null) return 0;
if( Math.abs((cal(root.left)+1)-(cal(root.right)+1))>1)
{
return 100000;
}
else{
return Math.max((cal(root.left)+1),(cal(root.right)+1));
}
}
}
二叉树的层序遍历 II
class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> sum=new LinkedList<>();
if(root==null) return sum;
Queue<TreeNode> x=new LinkedList<>();
x.add(root);
while(!x.isEmpty())
{
TreeNode temp;
List<Integer> y=new ArrayList<>();
int count = x.size();
for (int i=0;i<count;i++) {
TreeNode node = x.poll();
y.add(node.val);
if (node.left != null)
x.add(node.left);
if (node.right != null)
x.add(node.right);
}
sum.add(y);
}
Collections.reverse(sum);
return sum;
}
}
二叉树的最大深度
class Solution {
public int maxDepth(TreeNode root) {
if(root==null) return 0;
return Math.max((maxDepth(root.left)+1),maxDepth(root.right)+1);
二叉树的层序遍历
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> sum=new LinkedList<>();
if(root==null) return sum;
Queue<TreeNode> queue=new LinkedList<>();
queue.add(root);
while(!queue.isEmpty())
{
int count=queue.size();
List<Integer> x=new ArrayList<>();
TreeNode temp;
for(int i=0;i<count;i++)
{
temp=queue.peek();
queue.remove();
x.add(temp.val);
if(temp.left!=null) queue.add(temp.left);
if(temp.right!=null) queue.add(temp.right);
}
sum.add(x);
}
return sum;
}
}
对称二叉树
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null) return true;
return cal(root.left,root.right);
}
public boolean cal(TreeNode node1,TreeNode node2)
{
if(node1==null&&node2==null) return true;
if(node1==null||node2==null||node1.val!=node2.val) return false;
return cal(node1.left,node2.right)&&cal(node1.right,node2.left);
}
}
左叶子之和
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
int x=0;
if(root==null) return 0;
if(root.left!=null)
{
if(root.left.left==null&&root.left.right==null)
{
x=x+root.left.val;
}
}
x=x+sumOfLeftLeaves(root.left)+sumOfLeftLeaves(root.right);
return x;
}
}
二叉搜索树中的众数
class Solution {
int preVal = 0, curTimes = 0, maxTimes = 0;
ArrayList<Integer> list = new ArrayList<Integer>();
public int[] findMode(TreeNode root) {
ArrayList<Integer> sum = new ArrayList<Integer>();
sum=cal(root);
int size = sum.size();
int[] ans = new int[size];
for(int i = 0; i < size; i++){
ans[i] = sum.get(i);
}
return ans;
}
public ArrayList<Integer> cal(TreeNode root)
{
if(root==null) return list;
cal(root.left);
if(preVal == root.val){
curTimes++;
}else{
preVal = root.val;
curTimes = 1;
}
//判断当前数量与最大数量的关系, 更新 list 和 maxTimes
if(curTimes == maxTimes){
list.add(root.val);
}
else if(curTimes > maxTimes){
list.clear();
list.add(root.val);
maxTimes = curTimes;
}
cal(root.right);
return list;
}
}
二叉搜索树中的最小绝对值差
class Solution {
private int result = Integer.MAX_VALUE;
private TreeNode preNode = null;
public int getMinimumDifference(TreeNode root) {
return getMin(root);
}
public int getMin(TreeNode root)
{
if(root == null){
return result;
}
getMin(root.left);
if(preNode != null)
{
result = Math.min(Math.abs(root.val - preNode.val), result);
}
preNode = root;
getMin(root.right);
return result;
}
}
2021.9.3刷题
最新推荐文章于 2024-05-20 20:08:58 发布