14 July 2019
Angular (29) -- Modules and Services
by Jerry Zhang
LeetCode Day 9: P110. Balanced Binary Tree (Easy)
题目
给一个二叉树,判断它是否是平衡的。平衡二叉树的定义: 每个节点的子树高度差不超过1
我的思路:
这个题以前算法课学过,只要递归地算出各个节点的子树深度差就可以了。
我的代码:
public class E_110_BalancedBinaryTree {
public static boolean isBalanced(TreeNode root) {
if (root == null) {
return true;
}
return Math.abs(hight(root.left) - hight(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
}
private static int hight(TreeNode root) {
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
return 1;
}
return Math.max(hight(root.left), hight(root.right)) + 1;
}
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(2);
root.left.left = new TreeNode(3);
root.right.right = new TreeNode(3);
root.left.left.left = new TreeNode(4);
root.right.right.right = new TreeNode(4);
boolean balanced = isBalanced(root);
System.out.println("balanced = " + balanced);
}
}
坑:一开始没检查左右子树的平衡性,只检查了左右子树的高度差小于等于1。
最优解:
class Solution {
public boolean isBalanced(TreeNode root) {
if (root == null) {
return true;
}
return getHeight(root) != -1;
}
private int getHeight(TreeNode root) {
if (root == null) {
return 0;
}
int leftHeight = getHeight(root.left);
if (leftHeight == -1) {
return -1;
}
int rightHeight = getHeight(root.right);
if (rightHeight == -1) {
return -1;
}
if (Math.abs(leftHeight - rightHeight) > 1) {
return -1;
}
return Math.max(leftHeight, rightHeight) + 1;
}
}
这个解法是说,获取一个树的高度时,如果左子树的高度和右子树的高度差大于1,就返回一个-1作为标记。任何一个节点如果检测到这个标记, 就返回-1。最后只有当没有这个标记时,才是平衡的。
这个解法把测高度和异常情况的信息同时返回了,正常情况下直接返回高度,异常情况返回-1,非常高效。
Angular
Modules and Services
We can add services in four places:
- AppModule: available application wide. Using root injector.
- AppComponent(or other Components): available inside this component tree. Using component specific injector.
- Eager-loaded module: available application wide. (Not recommended)
- Lazy-loaded Module: only available in this lazy-loaded module. Using a separate child injector for this module. Only do this if you deliberately want to have a separate instance of that service.
Demo
Test 1:
First, we create a logging service in the root component.
@Injectable({ providedIn: 'root'})
export class LoggingService {
lastlog: string;
printLog(message: string) {
console.log(message);
console.log(this.lastlog);
this.lastlog = message;
}
}
Second, we inject the loggingService into the app component.
export class AppComponent implements OnInit {
constructor(private authService: AuthService, private loggingService: LoggingService) {}
ngOnInit(): void {
this.authService.autoLogin();
this.loggingService.printLog('Hello form AppComponent ngOnInit');
}
}
Then, we inject this service into the shopping list component. (I removed some irrelevant code)
export class ShoppingListComponent implements OnInit, OnDestroy {
constructor(private loggingService: LoggingService) { }
ngOnInit() {
this.loggingService.printLog('Hello from shoppingList component.');
}
}
When we go to the shopping list component, in the console we can see that the service instance reserves the message from the home page, which prove that in the entire application, we are using the same service instance.
Test 2:
If we put the LoggingService as a provider in the app.module, the output is the same.
Test 3:
Put the service in a eagerly loaded module, for example, CoreModule. The output is the same.
Test 4:
Provide the service in the app.module, but this time, also provide it in the shopping-list module, which is a lazy loading module.
This time the output is different. Now we are using two different objects, mean two different instances of that same service.
Test 5:
If we put the service in the shared module, which is imported both in app module and shopping list module. So it is both eagerly loaded and lazy loaded.
The result of this is that in the lazy loaded module, it has its own service instance.
Therefore, we should always put services in the app module if we do not have strong reason for not doing this.
Ahead-of-Time vs Just-in-Time Compilation
ng build --prod
This command generates a dist
folder. This folder contains all the minimized JS code.