leetcode打卡-二叉树II

目录

106. 从中序与后序遍历序列构造二叉树

112. 路径总和

404. 左叶子之和

513. 找树左下角的值

530. 二叉搜索树的最小绝对差

235. 二叉搜索树的最近公共祖先

 98. 验证二叉搜索树

701. 二叉搜索树中的插入操作

700. 二叉搜索树中的搜索

108. 将有序数组转换为二叉搜索树

236. 二叉树的最近公共祖先

 501. 二叉搜索树中的众数

538. 把二叉搜索树转换为累加树

669. 修剪二叉搜索树

 450. 删除二叉搜索树中的节点

 257. 二叉树的所有路径

110. 平衡二叉树

617. 合并二叉树

 654. 最大二叉树


106. 从中序与后序遍历序列构造二叉树

leetcode题目链接:https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal

leetcode AC记录:

思路:中序遍历是左中右,后序遍历是左右中,按照这个顺序后序数组的末尾为根,然后找到根在中序数组中的位置,将中序数组分为左右子树,递归循环这个过程即可。

代码如下:

public TreeNode buildTree(int[] inorder, int[] postorder) {
        return buildTree(inorder, postorder, 0, inorder.length-1, 0, postorder.length - 1);
    }

    public TreeNode buildTree(int[] inorder, int[] postorder, int inBegin, int inEnd, int postBegin, int postEnd) {
        if(inBegin > inEnd || postBegin > postEnd) {
            return null;
        }

        TreeNode root = new TreeNode();
        root.val = postorder[postEnd];
        int rootIndex = getRootIndex(inorder, root.val, inBegin, inEnd);
        root.left = buildTree(inorder, postorder, inBegin, rootIndex-1 , postBegin, postBegin + rootIndex - inBegin -1);
        root.right = buildTree(inorder, postorder, rootIndex + 1, inEnd , postBegin + rootIndex - inBegin, postEnd - 1);
        return root;
    }

    public int getRootIndex(int[] arr, int val, int begin, int end) {
        while(begin <= end) {
            if(arr[begin] == val) {
                return begin;
            }
            begin++;
        }
        return -1;
    }

112. 路径总和

leetcode题目链接:https://leetcode.cn/problems/path-sum

leetcode AC记录: 

思路:使用回溯和递归,将当前节点的累加和传递到子树中,如果碰到叶子节点,判断和是否为目标值,如果是返回并将结果递归到上层,层层递归最终返回。

代码如下:

 public boolean hasPathSum(TreeNode root, int targetSum) {
        return pathSum(root, 0, targetSum);
    }

    public boolean pathSum(TreeNode node, int currentSum, int targetSum) {
        if(node == null) {
            return false;
        }
        currentSum += node.val;
        if(node.left == null && node.right == null && currentSum == targetSum) {
            return true;
        }

        boolean flag = false;
        if(node.left != null) {
            flag = pathSum(node.left, currentSum, targetSum);
            if(flag == true) {
                return true;
            }
        }

        if(node.right != null) {
            flag = pathSum(node.right, currentSum, targetSum);
            if(flag == true) {
                return true;
            }
        }

        return flag;
    }

404. 左叶子之和

leetcode题目链接:https://leetcode.cn/problems/sum-of-left-leaves/

leetcode AC记录:

思路:使用递归方式前序遍历,返回条件为遇到左右子树为空,如果判断遇到了左叶子节点,则累加左叶子节点的值。

代码如下:

public int sumOfLeftLeaves(TreeNode root) {
        if(root == null) {
            return 0;
        }

        if(root.left == null && root.right == null) {
            return 0;
        }

        int leftSum = sumOfLeftLeaves(root.left);
        if(root.left != null && root.left.left == null && root.left.right == null)  {
            leftSum = root.left.val;
        }

        int rightSum = sumOfLeftLeaves(root.right);

        return leftSum + rightSum;
    }

513. 找树左下角的值

leetcode题目链接:https://leetcode.cn/problems/find-bottom-left-tree-value/

leetcode AC记录:

思路:层序遍历,找到最后一层第一个元素即可。

代码如下:

public int findBottomLeftValue(TreeNode root) {
        Deque<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int res = root.val;
        while(!queue.isEmpty()) {
            int size = queue.size();
            int index = 0;
            while(index < size) {
                TreeNode node = queue.pollFirst();
                if(index == 0) {
                    res = node.val;
                }
                index++;

                if(node.left != null) {
                    queue.offer(node.left);
                }

                if(node.right != null) {
                    queue.offer(node.right);
                }
            }
        }
        return res;
    }

530. 二叉搜索树的最小绝对差

leetcode题目链接:https://leetcode.cn/problems/minimum-absolute-difference-in-bst

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值