zoukankan      html  css  js  c++  java
  • 基础算法和数据结构高频题 II

    DFS的两种理解方式:
    1. 按照实际执行顺序模拟 (适合枚举型DFS,下节课内容)
    2. 按照DFS的定义宏观理解 (适合分治型DFS,本节课内容)

    1 Convert BST to Greater Tree

        int sum = 0;
        public TreeNode convertBST(TreeNode root) {
            dfs(root);
            return root;
        }
        void dfs(TreeNode root) {
            if (root == null) {
                return;
            }
            dfs(root.right);
            sum = sum + root.val;
            root.val = sum;
            dfs(root.left);
        }
    View Code

    2 Inorder Successor in Binary Search Tree

        public TreeNode InorderSuccessor(TreeNode root, TreeNode p) {
            if (root == null || p == null) {
                return null;
            }
            
            if (root.val <= p.val) {
                return InorderSuccessor(root.right, p);
            } else {
                TreeNode left = InorderSuccessor(root.left, p);
                return left == null ? root, left;
            }
        }
    View Code

    3 Validate Binary Search Tree

        public boolean isValidBST(TreeNode root) 
        {
            return help(root, Long.MIN_VALUE, Long.MAX_VALUE);
        }
        boolean help(TreeNode root, long min, long max) {
            if (root == null) {
                return true;
            }
            if (root.val <= min || root.val >= max) {
                return false;
            }
            return help(root.left, min, root.val) && help(root.right, root.val, max);
        }
    View Code

    4Binary Tree Inorder Traversal

    public class Solution {
        /**
         * @param root: The root of binary tree.
         * @return: Inorder in ArrayList which contains node values.
         */
        public ArrayList<Integer> inorderTraversal(TreeNode root) {
            // write your code here
            ArrayList<Integer> res = new ArrayList<>();
            dfs(root, res);
            return res;
        }
        void dfs(TreeNode root, ArrayList<Integer> res) {
            if (root == null) {
                return;
            }
            dfs(root.left, res);
            res.add(root.val);
            dfs(root.right, res);
        }
     }
    View Code

    二叉树类问题

    5 Binary Tree Flipping

    TreeNode newRoot;
        void dfs(TreeNode cur) {
            if (cur.left == null) {
                newRoot = cur;
                return;
            }
            dfs(cur.left);
            cur.left.right = cur;
            cur.left.left = cur.left;
            cur.left = null;
            cur.right = null;
        }
        
        public TreeNodee upsideDownBinaryTree(TreeNode root) {
            if (root == null) {
                return root;
            }
            dfs(root);
            return newRoot;
        }
    View Code

    6 Binary Tree Leaves Order Traversal

        Map<Integer, List<Integer>> map = new HashMap<>();
        int dfs(TreeNode root) {
            if (root == null) {
                return 0;
            }
            int left = dfs(root.left);
            int right = dfs(root.right);
            int max = Math.max(left, right) + 1;
            if (!map.containsKey(max)) {
                map.put(max, new ArrayList<>());
            }
            map.get(max).add(root.val);
            return max;
        }
        
        public List<List<Integer>> findLeaves(TreeNode root) {
            List<List<Integer>> res = new ArrayList<>();
            if (root == null) {
                return res;
            }
            int max_deep = dfs(root);
            for (int i = 1; i <= max_deep; i++) {
                res.add(map.get(i));
            }
            return res;
        }
    View Code

    7Binary Tree Leaves Order Traversal

        public List<List<Integer>> virtalOrder(TreeNode root) {
            List<List<Integer>> res = new ArrayList<>();
            if (root == null) {
                return res;
            }
            Map<Integer, ArrayList<Integer>> map = new HashMap<>();
            Queue<Integer> c = new LinkedList<>();
            Queue<TreeNode> q = new LinkedList<>();
            c.offer(0);
            q.offer(root);
            while (!q.isEmpty()) {
                Integer l = c.poll();
                TreeNode node = q.poll();
                if (!map.containsKey(l)) {
                    map.put(l, new ArrayList<>());
                }
                map.get(l).add(node.val);
                if (node.left != null) {
                    c.offer(l - 1);
                    q.offer(node.left);
                }
                if (node.right != null) {
                    c.offer(l + 1);
                    q.offer(node.right);
                }
            }
            for (int i = Collections.min(map.keySet()); i <= Collections.max(map.keySet()); i++) {
                res.add(map.get(i));
            }
            return res;
        }
    View Code
  • 相关阅读:
    mysql 的事件之 Waiting for table metadata lock
    postgresl 10 的逻辑复制 logical replication 之一
    postgresql 物理备份 pg_rman
    pg_waldump pg_xlogdump 的初步使用
    centos 7 添加 aliyun yum 源、epel源
    pg_dump 迁移 prod 环境的 function 到 uat 环境
    POSTGRESQL 11 BETA 1 RELEASED!
    使用 mtools 搭架 mongodb 的自动化日志分析平台
    将xml转换为PHP数组
    php自动获取字符串编码函数mb_detect_encoding
  • 原文地址:https://www.cnblogs.com/whesuanfa/p/7749958.html
Copyright © 2011-2022 走看看