剑指Offer题目笔记15(二叉搜索树)

面试题52:

面试题52

问题:

​ 给定一棵二叉搜索树,调整节点的指针使每个节点都没有左子节点。

解决方案:

​ 使用中序遍历,因为二叉搜索树是左节点的值小于等于根节点,根节点小于等于右节点的值,所以要是向使用每个节点都没有左子树,那么就需要先遍历左节点。

源代码:
/**
 * 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 TreeNode increasingBST(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        TreeNode node = root;
        //记录当前节点的前一个节点
        TreeNode prev = null;
        //记录第一个节点
        TreeNode first = null;
        while(node != null || !stack.isEmpty()){
            while(node != null){
                stack.push(node);
                node = node.left;
            }

            node = stack.pop();
            if(prev != null){
                prev.right = node;
            }else{
                first = node;
            }

            prev = node;
            //将当前节点的左节点删除。
            node.left = null;
            node = node.right;
        }

        return first;
    }
}

面试题53:

面试题53

问题:

​ 给定一棵二叉树搜索树和它的一个节点p,找出按中序遍历的顺序该节点p的下一个节点。

解决方案:

​ 从根节点开始,每个节点都与节点p对比,如果当前节点的值小于节点p,那么节点p的下一个节点就位于当前节点的右子树中,如果当前节点的值大于节点p,那么节点p的下一个节点就可能是当前节点或者位于当前节点的左子树中,就先将当前节点记录下来,然后前往当前节点的左子树,确定是否存在大于节点p,但是小于当前节点的值。

源代码:
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        TreeNode result = null;
        TreeNode cur = root;
        while(cur != null){
            if(cur.val > p.val){
                result = cur;
                cur = cur.left;
            }else{
                cur = cur.right;
            }
        }

        return result;
    }
}

面试题54:

面试题54

问题:

​ 给定一棵二叉树搜索树,将它的每个节点的值替换成树中大于或等于该节点值的所有节点值之和。

解决方案:
  1. 使用中序遍历,先遍历一遍二叉树,求出所有节点值之和total,然后再使用中序遍历遍历二叉树,因为中序遍历是从小到大的顺序,故使用sum记录,再用total减去sum即可。
  2. 但是有没有只遍历一次二叉搜索树的方案呢?有!使用中序遍历,但是该中序遍历,先遍历右节点,然后根节点,然后左节点,达到从大到小的顺序,只需要使用sum加起来就行了。
源代码:
class Solution {
    public TreeNode convertBST(TreeNode root) {
        if(root == null){
            return null;
        }
        TreeNode cur = root;
        Stack<TreeNode> stack = new Stack<>();
        int sum = 0;
        while(cur != null || !stack.isEmpty()){
            while(cur != null){
                stack.push(cur);
                cur = cur.right;
            }

            cur = stack.pop();
            sum += cur.val;
            cur.val = sum;
            cur = cur.left;
        }
        return root;
    }
}

面试题55:

面试题55

问题:

​ 实现二叉搜索树的迭代器,补全3个函数。

解决方案:

​ 使用中序遍历,代码中有一个while循环,循环的条件为true时循环体每执行一次就遍历二叉树的一个节点。当while循环的条件为false时,二叉树中的所有节点都已遍历完。因此,中序遍历的迭代代码中的while循环可以看成迭代器hasNext的判断条件,而while循环体内执行的操作就是函数next执行的操作。

源代码:
/**
 * 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 BSTIterator {

    Stack<TreeNode> stack;
    TreeNode cur;
    public BSTIterator(TreeNode root) {
        cur = root;
        stack = new Stack<>();
    }
    
    public int next() {
        while(cur != null){
            stack.push(cur);
            cur = cur.left;
        }

        cur = stack.pop();
        int val = cur.val;
        cur = cur.right;
        
        return val;
    }
    
    public boolean hasNext() {
        return cur != null || !stack.isEmpty();
    }
}

面试题56:

面试题56

第一种方法(使用哈希表):

问题:

​ 给定一棵二叉搜索树和一个值k,判断该二叉搜索树是否存在值之和等于k的两个节点。

解决方案:

​ 使用哈希表保存节点的值,可以采用任意顺序遍历二叉搜索树,每遍历一个节点,判断哈希表中是否存在K-root.val的值,存在返回true,不存在返回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 findTarget(TreeNode root, int k) {
        Set<Integer> set = new HashSet<>();
        Stack<TreeNode> stack = new Stack<>();
        while(root != null || !stack.isEmpty()){
            while(root != null){
                stack.push(root);
                root = root.left;
            }

            root = stack.pop();
            if(set.contains(k-root.val)){
                return true;
            }

            set.add(root.val);
            root = root.right;
        }
        return false;
    }
}

第二种方法(使用双指针):

思路:

​ 使用双指针,面试题第6题介绍了利用双指针求排序数组中是否包含两个和为k的数字,即把第一个指针指向数组中最小的数,第二个指针指向数组中最大的数,利用第55题中的BSTIterator的迭代器构成第一个指针,因为可以按照从小到大的顺序从二叉搜索树取出节点。利用第55题中的BSTIterator的迭代器并颠倒顺序构成第二个指针,因为可以按照从大到小的顺序从二叉搜索树取出节点。

源代码:
/**
 * 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 {
    class BSTIterator {

        Stack<TreeNode> stack;
        TreeNode cur;
        public BSTIterator(TreeNode root) {
            cur = root;
            stack = new Stack<>();
        }
        
        public int next() {
            while(cur != null){
                stack.push(cur);
                cur = cur.left;
            }

            cur = stack.pop();
            int val = cur.val;
            cur = cur.right;
            
            return val;
        }
        
        public boolean hasNext() {
            return cur != null || !stack.isEmpty();
        }
    }

    class BSTIteratorReversed {

        Stack<TreeNode> stack;
        TreeNode cur;
        public BSTIteratorReversed(TreeNode root) {
            cur = root;
            stack = new Stack<>();
        }
        
        public int prve() {
            while(cur != null){
                stack.push(cur);
                cur = cur.right;
            }

            cur = stack.pop();
            int val = cur.val;
            cur = cur.left;
            
            return val;
        }
        
        public boolean hasPrev() {
            return cur != null || !stack.isEmpty();
        }
    }

    public boolean findTarget(TreeNode root, int k) {
        if(root == null){
            return false;
        }

        BSTIterator iterNext = new BSTIterator(root);
        BSTIteratorReversed iterPrve = new BSTIteratorReversed(root);

        int next = iterNext.next();
        int prve = iterPrve.prve();
        while(next != prve){
            if(next + prve == k){
                return true;
            }

            if(next + prve > k){
                prve = iterPrve.prve();
            }else{
                next = iterNext.next();
            }
        }
        return false;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值