Skip to content

Commit ce82bb7

Browse files
add java solution for BST
1 parent 46b71e1 commit ce82bb7

File tree

4 files changed

+112
-3
lines changed

4 files changed

+112
-3
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@
212212
|238|[Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/)| |Medium|
213213
|237|[Node in a Linked List](https://leetcode.com/problems/-node-in-a-linked-list/)| [js](./algorithms/nodeInALinkedList/nodeInALinkedList.js),[java](./algorithms/nodeInALinkedList/Solution.java) |Easy|
214214
|236|[Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/)| |Medium|
215-
|235|[Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)| |Easy|
215+
|235|[Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)| [java](./algorithms/lowestCommonAncestorOfBST/Solution.java) |Easy|
216216
|234|[Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/)| [java](./algorithms/palindromeLinkedList/Solution.java) |Easy|
217217
|233|[Number of Digit One](https://leetcode.com/problems/number-of-digit-one/)| |Medium|
218218
|232|[Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/)| |Easy|
@@ -320,7 +320,7 @@
320320
|124|[Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/)| |Hard|
321321
|123|[Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/)| |Hard|
322322
|122|[Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)| [js](./algorithms/bestTimeToBuyAndSellStockIi/Solution.js) |Medium|
323-
|121|[Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)| |Medium|
323+
|121|[Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)| [java](./algorithms/bestTimeToBuyAndSellStock/Solution.java) |Medium|
324324
|120|[Triangle](https://leetcode.com/problems/triangle/)| |Medium|
325325
|119|[Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii/)| |Easy|
326326
|118|[Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)| |Easy|
@@ -337,7 +337,7 @@
337337
|107|[Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii/)| |Easy|
338338
|106|[Construct Binary Tree from Inorder and Postorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/)| |Medium|
339339
|105|[Construct Binary Tree from Preorder and Inorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)| |Medium|
340-
|104|[Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/)| [js](./algorithms/maximumDepthOfBinaryTree/maximumDepthOfBinaryTree.js) |Easy|
340+
|104|[Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/)| [js](./algorithms/maximumDepthOfBinaryTree/maximumDepthOfBinaryTree.js) [java](./algorithms/maximumDepthOfBinaryTree/Solution.java) |Easy|
341341
|103|[Binary Tree Zigzag Level Order Traversal](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/)| |Medium|
342342
|102|[Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/)| [js](./algorithms/binaryTreeLevelOrderTraversal/binaryTreeLevelOrderTraversal.js) |Easy|
343343
|101|[Symmetric Tree](https://leetcode.com/problems/symmetric-tree/)| [java](./algorithms/sysmetricTree/Solution.java), [js](./algorithms/sysmetricTree/Solution.js) |Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class Solution {
2+
public int maxProfit(int prices[]) {
3+
int minprice = Integer.MAX_VALUE;
4+
int maxprofit = 0;
5+
for (int i = 0; i < prices.length; i++) {
6+
if (prices[i] < minprice) {
7+
minprice = prices[i];
8+
} else if (prices[i] - minprice > maxprofit) {
9+
maxprofit = prices[i] - minprice;
10+
}
11+
}
12+
return maxprofit;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
11+
class Solution {
12+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
13+
List<TreeNode> pPath = getPath(root,p);
14+
List<TreeNode> qPath = getPath(root,q);
15+
16+
TreeNode common = null;
17+
for(int i=0; i<pPath.size()&&i<qPath.size();i++){
18+
if(pPath.get(i) == qPath.get(i)){
19+
common = pPath.get(i);
20+
}else{
21+
break;
22+
}
23+
}
24+
return common;
25+
}
26+
27+
28+
public List<TreeNode> getPath(TreeNode root, TreeNode target){
29+
List<TreeNode> treeNodePath = new ArrayList();
30+
TreeNode node = root;
31+
while(node!=target){
32+
treeNodePath.add(node);
33+
if(node.val > target.val){
34+
node = node.left;
35+
}else{
36+
node = node.right;
37+
}
38+
}
39+
treeNodePath.add(target);
40+
return treeNodePath;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
//递归
18+
public int maxDepth(TreeNode root) {
19+
if(null == root){
20+
return 0;
21+
}
22+
int rightMax = maxDepth(root.right);
23+
int leftMax = maxDepth(root.left);
24+
return Math.max(rightMax,leftMax)+1;
25+
}
26+
27+
//广度遍历
28+
public int maxDepth(TreeNode root){
29+
if(root == null){
30+
return 0;
31+
}
32+
Queue<TreeNode> queue = new LinkedList<>();
33+
queue.offer(root);
34+
int res = 0;
35+
36+
while(!queue.isEmpty()){
37+
//广度遍历核心是一层一层的进行
38+
int size = queue.size();
39+
while(size>0){
40+
TreeNode node = queue.poll();
41+
if(node.left !=null){
42+
queue.offer(node.left);
43+
}
44+
if(node.right !=null){
45+
queue.offer(node.right);
46+
}
47+
size--;
48+
}
49+
res++;
50+
}
51+
return res;
52+
}
53+
}

0 commit comments

Comments
 (0)