leetcode二叉树专题总结(一)

1.给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素。
说明:你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution {
    private int i ,ans = 0;
    public int kthSmallest(TreeNode root, int k) {
        i = k;
        inorder(root);
        return ans;
    }
    private void inorder(TreeNode root){
        if(root == null) return;
        inorder(root.left);
        if(--i == 0) ans = root.val;
        inorder(root.right);
    }
}

2.给定一个二叉树,返回它的 前序 遍历。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/binary-tree-preorder-traversal/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution {
    private List<Integer> list = new ArrayList<Integer>();
    public List<Integer> preorderTraversal(TreeNode root) {
        preorder(root);
        return list;
    }
    private void preorder(TreeNode root){
        if(root == null) return;
        list.add(root.val);
        preorder(root.left);
        preorder(root.right);
    }
}

给定一个二叉树,返回它的中序 遍历。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution {
    private List<Integer> list = new ArrayList<Integer>();
    public List<Integer> inorderTraversal(TreeNode root) {
        inorder(root);
        return list;
    }
    private void inorder(TreeNode root){
        if(root == null) return;
        inorder(root.left);
        list.add(root.val);
        inorder(root.right);
    }
}

给定一个二叉树,返回它的 后序 遍历。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/binary-tree-postorder-traversal/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution {
    private List<Integer> list = new ArrayList<Integer>();
    public List<Integer> postorderTraversal(TreeNode root) {
        postorder(root);
        return list;
    }
    private void postorder(TreeNode root){
        if(root == null) return;
        postorder(root.left);
        postorder(root.right);
        list.add(root.val);
    }
}

3.给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null || p == root || q == root) return root;
        TreeNode left = lowestCommonAncestor(root.left,p,q);
        TreeNode right = lowestCommonAncestor(root.right,p,q);
        if(left == null && right == null) return null;//如果左右子树返回结果都为null,则没有最近公共祖先
        if(left == null) return right;//如果左子树返回结果为null,返回右子树的查询结果
        if(right == null) return left;//如果右子树返回结果为null,返回左子树的查询结果
        return root;    //如果左右子树都不为null,返回目前节点
    }
}

4.给定一个二叉树,判断其是否是一个有效的二叉搜索树。
假设一个二叉搜索树具有如下特征:
节点的左子树只包含小于当前节点的数。
节点的右子树只包含大于当前节点的数。
所有左子树和右子树自身必须也是二叉搜索树。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/validate-binary-search-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution {
//第一种方法
    // private List<Integer> list = new ArrayList<Integer>();
    // public boolean isValidBST(TreeNode root) {
    //     inorder(root);
    //     for(int i=0;i<list.size()-1;i++){
    //         if(list.get(i)>=list.get(i+1)) return false;
    //     }
    //     return true;
    // }
    // private void inorder(TreeNode root){
    //     if(root == null) return;
    //     inorder(root.left);
    //     list.add(root.val);
    //     inorder(root.right);
    // }
 //第二种方法
    double last = -Double.MAX_VALUE;
    public boolean isValidBST(TreeNode root){
        if(root == null) return true;
        if(isValidBST(root.left)){
            if(last < root.val){
                last = root.val;
                return isValidBST(root.right);
            }
        }
        return false;
    }
}

5.给定两个二叉树,编写一个函数来检验它们是否相同。
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/same-tree/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p == null && q == null) return true;
		if(p != null && q != null && p.val == q.val)
		{
			return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
		}else return false;
    }
}

6.给定一个二叉树,检查它是否是镜像对称的。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/symmetric-tree/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution {
    public boolean isSymmetric(TreeNode root) {
        return same(root,root);
    }
    private boolean same(TreeNode p,TreeNode q){
        if(p == null && q == null) return true;
        if(p != null && q != null && p.val == q.val){
            return same(p.left,q.right) && same(p.right,q.left);
        }else return false;
    }
}

7.给定一个二叉树,返回其节点值的锯齿形层次遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if(root == null){
            return res;
        }
        boolean rg =false;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            List<Integer> list = new ArrayList<>();
            while(size-- >0){
                TreeNode node = queue.poll();
                if(node.left != null){
                    queue.offer(node.left);
                }
                if(node.right != null){
                    queue.offer(node.right);
                }
                if(rg){
                    list.add(0,node.val);//在指定位置插入元素,后面的元素都往后移一个元素。
                }else{
                    list.add(node.val);
                }
            }
            rg = !rg;
            res.add(list);
        }
        return res;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值