二叉树 - 20190205

二叉树时间复杂度的训练:

T(n) = 2T(n/2)+o(n)  nlogN 归并排序

快排:平均nlogN 最坏 n^2

树形展开法

T(N) = 2T(N/2)+O(1)   o(n)

二叉树的时间复杂度= N*每个节点的处理时间

前序便利:根左右

中序 左根右

后序:左右根

DFS深度优先搜索:1.分治法 2.遍历法

递归的步骤:

1.定义 2.拆解 3.什么时候返回

divide & conquere:

定义的函数有return value

https://www.lintcode.com/problem/binary-tree-preorder-traversal/description

 1 /**
 2  * Definition of TreeNode:
 3  * public class TreeNode {
 4  *     public int val;
 5  *     public TreeNode left, right;
 6  *     public TreeNode(int val) {
 7  *         this.val = val;
 8  *         this.left = this.right = null;
 9  *     }
10  * }
11  */
12 
13 public class Solution {
14     /**
15      * @param root: A Tree
16      * @return: Preorder in ArrayList which contains node values.
17      */
18     public List<Integer> preorderTraversal(TreeNode root) {
19        List<Integer> result = new ArrayList();
20        if(root==null){
21            return result;
22        }
23        
24        List<Integer> left = preorderTraversal(root.left);
25        List<Integer> right = preorderTraversal(root.right);
26        
27        result.add(root.val);
28        result.addAll(left);
29        result.addAll(right);
30        return result;
31     }
32 }
View Code

经典的分治算法 merge sort/quick sort

90%的二叉树都可以用divide&conquere的方法

https://www.lintcode.com/problem/maximum-depth-of-binary-tree/description

 1 /**
 2  * Definition of TreeNode:
 3  * public class TreeNode {
 4  *     public int val;
 5  *     public TreeNode left, right;
 6  *     public TreeNode(int val) {
 7  *         this.val = val;
 8  *         this.left = this.right = null;
 9  *     }
10  * }
11  */
12 
13 public class Solution {
14     /**
15      * @param root: The root of binary tree.
16      * @return: An integer
17      */
18     public int maxDepth(TreeNode root) {
19         // write your code here
20         if(root == null){
21             return 0;
22         }
23         
24         int left = maxDepth(root.left);
25         int right = maxDepth(root.right);
26         return Math.max(left,right)+1;
27     }
28 }
View Code

二叉树问题一般return是root时return,但也有例外

https://www.lintcode.com/problem/binary-tree-paths/description 这道题需要处理叶子节点

 1 /**
 2  * Definition of TreeNode:
 3  * public class TreeNode {
 4  *     public int val;
 5  *     public TreeNode left, right;
 6  *     public TreeNode(int val) {
 7  *         this.val = val;
 8  *         this.left = this.right = null;
 9  *     }
10  * }
11  */
12 
13 public class Solution {
14     /**
15      * @param root: the root of the binary tree
16      * @return: all root-to-leaf paths
17      */
18     public List<String> binaryTreePaths(TreeNode root) {
19         List<String> paths = new ArrayList<>();
20         // write your code here 找到所有方案 都是用搜索
21         if(root==null){
22             return paths;
23         }
24         
25         //这道题需要处理叶子节点
26         if(root.left==null && root.right==null){
27             paths.add(root.val + "");
28             return paths;
29         }
30         
31         //2.拆解
32         List<String> leftPaths = binaryTreePaths(root.left);
33         List<String> rightPaths = binaryTreePaths(root.right);
34         
35         //merge
36         for(String path:leftPaths){
37             paths.add(root.val + "->" + path);
38         }
39         
40         for(String path:rightPaths){
41             paths.add(root.val + "->" + path);
42         }
43         
44         return paths;
45     }
46 }
View Code

https://www.lintcode.com/problem/minimum-subtree/description

 1 /**
 2  * Definition of TreeNode:
 3  * public class TreeNode {
 4  *     public int val;
 5  *     public TreeNode left, right;
 6  *     public TreeNode(int val) {
 7  *         this.val = val;
 8  *         this.left = this.right = null;
 9  *     }
10  * }
11  */
12 
13 public class Solution {
14     /**
15      * @param root: the root of binary tree
16      * @return: the root of the minimum subtree
17      */
18      private int subtreeSum = Integer.MAX_VALUE;
19      private TreeNode subtree = null;
20     public TreeNode findSubtree(TreeNode root) {
21         // write your code here
22         helper(root);
23         return subtree;
24     }
25     
26     private int helper(TreeNode root){
27         if(root == null){
28             return 0;
29         }
30         
31         int sum = helper(root.left) + helper(root.right) + root.val;
32         if(sum <subtreeSum){
33             subtreeSum = sum;
34             subtree = root;
35         }
36         
37         return sum;
38     }
39 }
View Code

result type

treeMap 就是平衡二叉树

定义:对于每一个节点 左右高度不超过1

https://www.lintcode.com/problem/balanced-binary-tree/description

 1 /**
 2  * Definition of TreeNode:
 3  * public class TreeNode {
 4  *     public int val;
 5  *     public TreeNode left, right;
 6  *     public TreeNode(int val) {
 7  *         this.val = val;
 8  *         this.left = this.right = null;
 9  *     }
10  * }
11  */
12 
13 class ResultType{
14     public boolean isBalanced;
15     public int maxDepth;
16     public ResultType(boolean isBalanced,int maxDepth){
17         this.isBalanced = isBalanced;
18         this.maxDepth = maxDepth;
19     }
20 }
21 public class Solution {
22     /**
23      * @param root: The root of binary tree.
24      * @return: True if this Binary tree is Balanced, or false.
25      */
26      //根节点左右高度不超过1,且左右平衡
27     public boolean isBalanced(TreeNode root) {
28         // write your code here
29         return Helper(root).isBalanced;
30     }
31     
32     private ResultType Helper(TreeNode root){
33         if(root == null){
34             return new ResultType(true,0);
35         }
36         
37         ResultType left = Helper(root.left);
38         ResultType right = Helper(root.right);
39         
40         if(!left.isBalanced || !right.isBalanced ||Math.abs(left.maxDepth-right.maxDepth)>1){
41             return new ResultType(false,-1);
42         }
43         
44         return new ResultType(true,Math.max(left.maxDepth,right.maxDepth)+1);
45     }
46 }
View Code

LCA 最近公共祖先

https://www.lintcode.com/problem/lowest-common-ancestor-of-a-binary-tree/description

 1 /**
 2  * Definition of TreeNode:
 3  * public class TreeNode {
 4  *     public int val;
 5  *     public TreeNode left, right;
 6  *     public TreeNode(int val) {
 7  *         this.val = val;
 8  *         this.left = this.right = null;
 9  *     }
10  * }
11  */
12 
13 
14 public class Solution {
15     /*
16      * @param root: The root of the binary tree.
17      * @param A: A TreeNode
18      * @param B: A TreeNode
19      * @return: Return the LCA of the two nodes.
20      */
21     //A&B -> LCA
22     //!A&!B ->null
23     //A & !B ->A
24     //B&!A ->B
25     public TreeNode lowestCommonAncestor(TreeNode root, TreeNode A, TreeNode B) {
26         // write your code here
27         if(root ==null || root==A || root ==B){
28             return root;
29         }
30         
31         TreeNode left = lowestCommonAncestor(root.left,A,B);
32         TreeNode right = lowestCommonAncestor(root.right,A,B);
33         
34         if(left!=null && right !=null){
35             return root;
36         }
37         
38         //lca or a or b
39         if(left!=null){
40             return left;
41         }
42         
43         if(right!=null){
44             return right;
45         }
46         return null;
47     }
48 }
View Code

https://www.lintcode.com/problem/validate-binary-search-tree/description

 1 /**
 2  * Definition of TreeNode:
 3  * public class TreeNode {
 4  *     public int val;
 5  *     public TreeNode left, right;
 6  *     public TreeNode(int val) {
 7  *         this.val = val;
 8  *         this.left = this.right = null;
 9  *     }
10  * }
11  */
12 
13 class ResultType{
14     boolean isValidBST;
15     int min, max;
16     public ResultType(boolean isValidBST,int min, int max){
17         this.isValidBST = isValidBST;
18         this.min = min;
19         this.max = max;
20     }
21 }
22 
23 public class Solution {
24     /**
25      * @param root: The root of binary tree.
26      * @return: True if the binary tree is BST, or false
27      */
28     public boolean isValidBST(TreeNode root) {
29         // write your code here
30         return Helper(root).isValidBST;
31     }
32     
33     public ResultType Helper(TreeNode root){
34         if(root == null){
35             return new ResultType(true, Integer.MAX_VALUE,Integer.MIN_VALUE);
36         }
37         
38         ResultType left = Helper(root.left);
39         ResultType right = Helper(root.right);
40         
41         if(!left.isValidBST ||(root.left!=null && left.max>= root.val)){
42             return new ResultType(false,0,0);
43         }
44         
45         if(!right.isValidBST ||(root.right!=null && right.min<= root.val)){
46             return new ResultType(false,0,0);
47         }
48         
49         return new ResultType(true,Math.min(root.val, left.min),Math.max(root.val,right.max));
50     }
51 }
View Code

猜你喜欢

转载自www.cnblogs.com/lizzyluvcoding/p/10353389.html