leetcode-month2-week7

来源:互联网 发布:大卫芬奇电影知乎 编辑:程序博客网 时间:2024/06/18 00:47

Binary Tree Level Order Traversal II

package ygy.test.week7;import java.util.*;/** * Created by guoyao on 2017/10/13. */public class BinaryTreeLevelOrderTraversalII {    public static void main(String[] args) {        TreeNode treeNode1=new TreeNode(3);        TreeNode treeNode2=new TreeNode(9);        TreeNode treeNode3=new TreeNode(7);        TreeNode treeNode4=new TreeNode(20);        TreeNode treeNode5=new TreeNode(15);        treeNode1.right=treeNode4;        treeNode1.left=treeNode2;        treeNode4.left=treeNode5;        treeNode4.right=treeNode3;        System.out.println(levelOrderBottom(treeNode1));    }    /**     * For example:     * Given binary tree [3,9,20,null,null,15,7],     * 3     * / \     * 9  20     * /  \     * 15   7     * return its bottom-up level order traversal as:     * [     * [15,7],     * [9,20],     * [3]     * ]     */    //使用广度优先算法    public  static  List<List<Integer>> levelOrderBottom(TreeNode root) {        List<List<Integer>> resultList=new ArrayList<>();        if (root == null) {            return resultList;        }        Queue<TreeNode> queue=new LinkedList<>();        queue.offer(root);        while (!queue.isEmpty()) {            List<Integer> childList=new ArrayList<>();            int size=queue.size();   //下一子节点个数            while (size-- > 0) {                TreeNode nextChild=queue.poll();                childList.add(nextChild.val);                if (nextChild.left != null) {                    queue.offer(nextChild.left);                }                if (nextChild.right != null) {                    queue.offer(nextChild.right);                }            }            resultList.add(childList);        }       Collections.reverse(resultList);        return resultList;    }}

Maximum Depth of Binary Tree

package ygy.test.week7;import java.util.LinkedList;import java.util.Queue;import java.util.Stack;/** * Created by guoyao on 2017/10/13. */public class MaximumDepthofBinaryTree {    public static void main(String[] args) {        TreeNode treeNode1=new TreeNode(1);        TreeNode treeNode2=new TreeNode(2);        TreeNode treeNode3=new TreeNode(3);        TreeNode treeNode4=new TreeNode(4);        TreeNode treeNode5=new TreeNode(5);        treeNode1.left=treeNode2;        treeNode1.right=treeNode3;        treeNode3.left=treeNode4;        treeNode2.right=treeNode5;        System.out.println(maxDepth(treeNode1));    }    /**     * Given a binary tree, find its maximum depth.     The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.     */    public static int maxDepth(TreeNode root) {        if (root == null) {            return 0;        }        int leftsum  = 1 ;        int rightsum  = 1 ;        leftsum+= maxDepth(root.left);     //计算左边的深度        rightsum+= maxDepth((root.right));  //计算右边的深度        return Math.max(leftsum, rightsum);  //取最大深度    }    //leetcode  bfs 广度优先搜素    public int maxDepth_bfs(TreeNode root) {        if(root == null) {            return 0;        }        Queue<TreeNode> queue = new LinkedList<>();        queue.offer(root);        int count = 0;        while(!queue.isEmpty()) {            int size = queue.size();    //将临节点全部取出            while(size-- > 0) {                TreeNode node = queue.poll();                if(node.left != null) {                    queue.offer(node.left);                }                if(node.right != null) {                    queue.offer(node.right);                }            }            count++;        }        return count;    }    //leetcode  深度优先    public int maxDepth_dfs(TreeNode root) {        if(root == null) {            return 0;        }        Stack<TreeNode> stack = new Stack<>();        Stack<Integer> value = new Stack<>();        stack.push(root);        value.push(1);        int max = 0;        while(!stack.isEmpty()) {            TreeNode node = stack.pop();            int temp = value.pop();            max = Math.max(temp, max);            if(node.left != null) {                stack.push(node.left);                value.push(temp+1);            }            if(node.right != null) {                stack.push(node.right);                value.push(temp+1);            }        }        return max;    }}/** * Definition for a binary tree node. */class TreeNode {    int val;    TreeNode left;    TreeNode right;    TreeNode(int x) {        val=x;    }}

Balanced Binary Tree

package ygy.test.week7;/** * Created by guoyao on 2017/10/15. */public class BalancedBinaryTree {    public static void main(String[] args) {        TreeNode treeNode1=new TreeNode(0);        TreeNode treeNode2=new TreeNode(2);        TreeNode treeNode3=new TreeNode(3);        treeNode1.right = treeNode2;        treeNode2.right=treeNode3;        System.out.println(isBalanced(treeNode1));    }    /**     * Given a binary tree, determine if it is height-balanced.     */    private static final int UNBALANCED = -99;    //leetcode answer    public static boolean isBalanced(TreeNode root) {        if (root == null) {            return true;        }        return getHeight(root) != UNBALANCED;    }    private static int getHeight(TreeNode root) {        if (root == null) {            return -1;        }        int l = getHeight(root.left);        int r = getHeight(root.right);        //左右深度的绝对值大于1则为非avl树        if (l == UNBALANCED || r == UNBALANCED || Math.abs(l-r) > 1) {            return UNBALANCED;        }        return 1 + Math.max(l,r);   //左右深度    }}

Convert Sorted Array to Binary Search Tree

package ygy.test.week7;/** * Created by guoyao on 2017/10/15. */public class ConvertSortedArraytoBinarySearchTree {    public static void main(String[] args) {    }    /**     * Given an array where elements are sorted in ascending order,     * convert it to a height balanced BST.     */    public static TreeNode sortedArrayToBST(int[] nums) {       if(nums == null || nums.length == 0) return null;        return  makeTree(nums, 0, nums.length - 1);    }    //利用二分查找分离    public static TreeNode makeTree(int[] nums, int i, int j) {        if(i > j) return  null ;        int mid=(i + j) >> 1;        TreeNode root=new TreeNode(nums[mid]);        root.left=makeTree(nums, i, mid - 1);        root.right=makeTree(nums, mid + 1, j);        return root ;    }}
原创粉丝点击