剑指offer 2021/7/31

43 篇文章 0 订阅
27 篇文章 0 订阅

55 - I. 二叉树的深度

法一:

递归实现DFS

class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null){
            return 0;
        }
        return Math.max(maxDepth(root.left), maxDepth(root.right))+1;
    }
}

法二:

BFS常规写法,但是要注意一层遍历结束之后res再执行加一操作。

class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null){
            return 0;
        }
        int res = 0;
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            //注意不能写为i < queue.size(),因为在循环内部队列的长度是不断变化的。
            int n = queue.size();
            for(int i = 0; i < n; ++i){
                TreeNode temp = queue.pop();
                if(temp.left!=null)
                    queue.add(temp.left);
                if(temp.right!=null)
                    queue.add(temp.right);
            }
            res += 1;
        }
        return res;
    }
}

32 - II. 从上到下打印二叉树 II

代码:

BFS

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> list = new LinkedList<>();
        LinkedList<TreeNode> queue = new LinkedList<>();
        if(root == null)
            return list;
        queue.add(root);
        while(!queue.isEmpty()){
            int n = queue.size();
            LinkedList<Integer> temp = new LinkedList<>();
            //可以改成for(int i = queue.size(); i > 0; --i)
            //这样就不用担心queue.size()在循环体中变化对代码造成影响
            for(int i = 0; i < n; ++i){
                TreeNode node = queue.pop();
                temp.add(node.val);
                if(node.left!=null)
                    queue.add(node.left);
                if(node.right!=null)
                    queue.add(node.right);
            }
            list.add(temp);
        }
        return list;
    }
}

18. 删除链表的节点

代码:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode deleteNode(ListNode head, int val) {
        if(head == null)
            return null;
        if(head.val == val)
            return head.next;
        ListNode cur = head;
        while(cur.next != null){
            if(cur.next.val == val){
                cur.next = cur.next.next;
                break;
            }
            cur = cur.next;
        }
        return head;
    }
}

33. 二叉搜索树的后序遍历序列

法一:

class Solution {
    public boolean verifyPostorder(int[] postorder) {
        return recur(postorder, 0, postorder.length-1);
    }
    boolean recur(int[] postorder, int i, int j){
        //i>=j说明只有一个节点,返回true
        if(i >= j)
            return true;
        int p = i;
        //找到第一个比根节点大的节点,以此为分界点划分左右子树
        while(postorder[p] < postorder[j])  p++;
        int m = p;
        //找到分界点右边第一个不大于根节点的数,并将其和根节点作比较。
        //目的是判断右子树是否都大于根节点
        while(postorder[p] > postorder[j])  p++;
        //最后递归判断左右子树是否符合要求
        return recur(postorder, i, m-1) && recur(postorder, m, j-1) && p==j;
    }
}

法二:

辅助栈

class Solution {
    public boolean verifyPostorder(int[] postorder) {
        Stack<Integer> stack = new Stack<>();
        int parent=Integer.MAX_VALUE;
        for(int i=postorder.length-1; i>=0; --i){
            if(postorder[i] > parent)
                return false;
            while(!stack.isEmpty() && stack.peek()>postorder[i]){
                parent=stack.pop();
            }
            stack.push(postorder[i]);
        }
        return true;
    }
}

68 - II. 二叉树的最近公共祖先

算法思想:

在左右子树中递归寻找p、q。若两个节点分别位于根节点两侧,则返回root;若位于左子树,则返回左子树的递归结果;若位于右子树,则返回右子树的递归结果。

代码:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
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)
            return right;
        if(right == null)
            return left;
        return root;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值