• This Leetcode & CTCI -- Day 7


    Question 1.

    Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

    Remember the middle index value should be the first node, while the left-side array be the node.right part and the right-side array be the node.left part. 
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public TreeNode sortedArrayToBST(int[] nums) {
            if (nums.length == 0)
                return null;
            return sortedArrayToBST(nums, 0, nums.length-1);
        }
        
        public TreeNode sortedArrayToBST(int[] nums, int smallIndex, int largeIndex){
            if (smallIndex > largeIndex)
                return null;
            int mid = (smallIndex + largeIndex)/2;
            TreeNode root = new TreeNode(nums[mid]);
            root.left = sortedArrayToBST(nums, smallIndex, mid-1);
            root.right = sortedArrayToBST(nums, mid+1, largeIndex);
            return root;
        }
    }
     

    Question 2

    Convert Sorted List to Binary Search Tree

    Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

    This is more direct solution, form bottom to top and find the middle node to be the root and then assign the root.left and root.right. One big difference between this problem to the above one is the linklist cannot directly get the middle node out but need to do recursive to get the middle one. So actually it store the from the first value to end, instead of getting the middle one firstly.

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        static ListNode head;
        public TreeNode sortedListToBST(ListNode head) {
            if (head == null)
                return null;
            this.head = head;
            return sortedListToBST(0, getSize(head)-1);
        }
        
        public TreeNode sortedListToBST( int smallIndex, int largeIndex){
            if (smallIndex > largeIndex)
                return null;
            int middle = (smallIndex + largeIndex) / 2;
            TreeNode left = sortedListToBST(smallIndex, middle-1);
            TreeNode root = new TreeNode(head.val);
            root.left = left;
            head = head.next;
            root.right = sortedListToBST(middle+1, largeIndex);
            return root;
        }
        
        public int getSize(ListNode head){
            int len = 0;
            while (head != null){
                head = head.next;
                len ++;
            }
            return len;
        }
    }

     Question 3

    Minimum Depth of Binary Tree

     Given a binary tree, find its minimum depth.

    The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

     This problem is done before with DFS function. Here I used BFS to do it again. For BFS, remember to use a queue to keep all nodes' siblings.

    public class Solution {
        public int minDepth(TreeNode root) {
            if (root == null)
                return 0;
            int depth = 1;
            ArrayList<TreeNode> queue = new ArrayList<TreeNode>();
            queue.add(root);
            int curnum = 1;
            int nextnum = 0;
            while (!queue.isEmpth()){
                TreeNode cur = queue.poll();
                curnum--;
                if (cur.left == null && cur.right == null)
                    return depth;
                if (cur.left != null){
                    queue.add(cur.left);
                    nextNum ++;
                }
                if (cur.right != null){
                    queue.add(cur.right);
                    nextnum ++;
                }
                if (curnum == 0){
                    curnum = nextnum;
                    nextnum = 0;
                    depth++;
                }
            }
            return depth;
        }
    }

     Another BSF code I found.

    public int minDepth(TreeNode root) {
            // BFS
            if (root == null) {
                return 0;
            }
            
            int res = 1;
            Queue<TreeNode> queue = new LinkedList<TreeNode>();
            queue.offer(root);
            while (!queue.isEmpty()) {
                int size = queue.size();
                while (size-- > 0) {
                    TreeNode node = queue.poll();
                    if (node.left == null && node.right == null) {
                        return res;
                    }
                    if (node.left != null) {
                        queue.offer(node.left);
                    }
                    if (node.right != null) {
                        queue.offer(node.right);
                    }
                }
                res++;
            }
            
            return res;
        }
  • 相关阅读:
    软件测试基础
    Python
    Python
    C# 打开帮助文档,打开电脑中其他应用或者文件
    C# 设置窗口大小为不可调、取消最大化、最小化窗口按键
    C# 控件置于最顶层、最底层、隐藏、显示
    C# 在窗口绘制图形(打点、画圆、画线)
    C# 不同窗口传递参数
    C# 禁止在textBox输入框输入非法字符
    C# 设定弹出窗体位置
  • 原文地址:https://www.cnblogs.com/timoBlog/p/4646754.html
Copyright © 2020-2023  润新知