码迷,mamicode.com
首页 > 编程语言 > 详细

基础算法和数据结构高频题 II

时间:2017-10-29 16:45:03      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:lap   while   treenode   success   null   view   list   write   splay   

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

 

基础算法和数据结构高频题 II

标签:lap   while   treenode   success   null   view   list   write   splay   

原文地址:http://www.cnblogs.com/whesuanfa/p/7749958.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!