二叉树相关题目

题目一:Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
For example:
Given the below binary tree and sum = 22,
              5
             / \
            4   8
           /   / \
          11  13  4
         /  \      \
        7    2      1

return true, as there exist a root-to-leaf path5->4->11->2which sum is 22.

public class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
        if(root==null)
            return false;
        if(root.left==null&&root.right==null)
            return sum==root.val?true:false;
        return hasPathSum(root.left,sum-root.val)||hasPathSum(root.right,sum-root.val);
    }
}

题目二:Path Sum2 

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.

import java.util.ArrayList;
public class Solution {
    ArrayList<ArrayList<Integer>> listAll=new ArrayList<ArrayList<Integer>>();
    ArrayList<Integer> list=new ArrayList<Integer>();
    
    public ArrayList<ArrayList<Integer>> pathSum(TreeNode root, int sum) {
        if(root==null)
            return listAll;
        sum-=root.val;
        list.add(root.val);
        if(sum==0&&root.left==null&&root.right==null)
            listAll.add(new ArrayList(list));
        
        pathSum(root.left,sum);
        pathSum(root.right,sum);
        list.remove(list.size()-1);
        return listAll;
        
    }
}

题目三:Path Sum3 

题目四: balanced binary tree

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

import java.util.LinkedList;
import java.util.Queue;

public class Solution {
    public boolean isBalanced(TreeNode root) {
        if(root==null||(root.left==null&&root.right==null))
            return true;
        Queue<TreeNode> q=new LinkedList<TreeNode>();
        q.offer(root);
        while(!q.isEmpty()){
            root=q.poll();
            if(root.left!=null)
                q.offer(root.left);
            if(root.right!=null)
                q.offer(root.right);
            
            int left=depth(root.left);
            int right=depth(root.right);
            if(Math.abs(left-right)>1)
                return false;
        }
        return true;

    }
    public static int depth(TreeNode root){
        if(root==null)
            return 0;
        int left=depth(root.left);
        int right=depth(root.right);
        return (left>right)?left+1:right+1;
    }
}
递归写法
public class Solution {
    public boolean isBalanced(TreeNode root) {
        if(root==null)
            return true;
        if(root.left==null && root.right==null)
            return true;
        if(Math.abs(depth(root.left)-depth(root.right))<=1 && isBalanced(root.left) && isBalanced(root.right))
            return true;
        else 
            return false;  
    }
    public static int depth(TreeNode root){
        if(root==null)
            return 0;
        if(root.left==null && root.right==null)
            return 1;
        return Math.max(depth(root.left),depth(root.right))+1;
        
    }
    
}

题目五:maximum depth of a binary tree

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

public class Solution {
    public int maxDepth(TreeNode root) {
        if(root==null)
            return 0;
        if(root.left==null&&root.right==null)
            return 1;
        return maxDepth(root.left)>maxDepth(root.right)? maxDepth(root.left)+1: maxDepth(root.right)+1;
    }
}

题目六:minimum depth of a binary tree

Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

//广度优先遍历
import java.util.LinkedList;
import java.util.Queue;
public class Solution {
    public int run(TreeNode root) {
        if(root==null)
            return 0;
        if(root.left==null&&root.right==null)
            return 1;
        Queue<TreeNode> q=new LinkedList<TreeNode>();
        q.offer(root);
        int depth=0;
        while(q!=null){
            depth++;
            int len=q.size();
            for(int i=0;i<len;++i){
                root=q.poll();
                if(root.left==null&&root.right==null)
                    return depth;
                if(root.left!=null){
                    q.offer(root.left);
                }
                if(root.right!=null)
                    q.offer(root.right); 
                
            }

        }
        return 0;
    }
}
public class Solution {
    public int run(TreeNode root) {
        //递归终止条件
        if(root==null)
            return 0;
        if(root.left==null && root.right==null)
            return 1;
        //递归过程
        //有一个陷阱 就是当左子树为空时,返回右子树的深度;右子树为空时返回左子树的深度;
        //都不为空时 返回二者的最小值
        if(root.left==null)
            return run(root.right)+1;
        if(root.right==null)
            return run(root.left)+1;
        
        return Math.min(run(root.left),run(root.right))+1;
        
    }
}

题目七:之字形层序遍历二叉树

import java.util.ArrayList;
import java.util.Stack;
public class Solution {
    public ArrayList<ArrayList<Integer>> zigzagLevelOrder(TreeNode root) {
        ArrayList<ArrayList<Integer>> listAll=new ArrayList<ArrayList<Integer>>();
        ArrayList<Integer> list=new ArrayList<Integer>();
        if(root==null)
            return listAll;
        //两个栈,用来存放当前和下一行的元素
        //这样写的目的是为了方便之后栈的交换
        ArrayList<Stack<TreeNode>> s=new ArrayList<Stack<TreeNode>>();
        s.add(new Stack<TreeNode>());
        s.add(new Stack<TreeNode>());
        int current=0;
        int next=1;
        int ltr=1;//标记入栈顺序;
        s.get(current).push(root);

        while(!s.get(current).isEmpty()){
            //遍历current栈中所有元素
            while(true){
                root=s.get(current).pop();
                list.add(root.val);
                //下一行入栈
                if(ltr==1){
                    if(root.left!=null)
                        s.get(next).push(root.left);
                    if(root.right!=null)
                        s.get(next).push(root.right);  
                }else if(ltr==0){
                    if(root.right!=null)
                        s.get(next).push(root.right);
                    if(root.left!=null)
                        s.get(next).push(root.left);
                }
                if(s.get(current).isEmpty()){
                    listAll.add(new ArrayList(list));
                    list.clear(); 
                    break;
                }

            }
           
            //交换两个栈
            current=1-current;
            next=1-next;
            ltr=1-ltr;//更改入栈顺序;
            
        }
              return listAll;

    }
}

题目八:层序遍历二叉树 并分层存储

import java.util.ArrayList;
import java.util.LinkedList;

public class Solution {
    public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
        ArrayList<Integer> list=new ArrayList<Integer>();
        ArrayList<ArrayList<Integer>> res=new ArrayList<ArrayList<Integer>>();
        
        if(root==null)
            return res;
        
        LinkedList<TreeNode> q=new LinkedList<TreeNode>();
        q.offer(root);
        q.offer(null);
        while(!q.isEmpty()){
            TreeNode temp=q.poll();
            if(temp==null){
                //遍历完了一层
                res.add(new ArrayList(list));
                list.clear();
                if(q.isEmpty()){
                    break;
                }
                q.offer(null);

            }else{
                list.add(temp.val);
                if(temp.left!=null)
                    q.offer(temp.left);
                if(temp.right!=null)
                    q.offer(temp.right);
            }
            
        }
        return res;
    }
}

题目九:对称的二叉树

题目十:二叉树的镜像

题目十一:二叉树和为某一值的路径

题目十二:反转二叉树

题目十三:二叉搜索树中两个结点最近公共祖先

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root==null)
            return null;
        //如果p q在二叉搜索树的同侧,则递归地在相应子树中寻找
        if(q.val<root.val && p.val<root.val)
            return lowestCommonAncestor(root.left, p, q);
        if(q.val>root.val && p.val>root.val)
            return lowestCommonAncestor(root.right, p, q);
        //否则 最近的公共祖先一定是root
        return root;
            
            
    }
}

题目十四:二叉树中两个结点的最近公共祖先

题目:验证是否为BST

题目:删除BST的一个结点

题目:将有序数组转化为BST

题目:BST中最小的K个元素


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值