forked from h2pl/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
197 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package 数据结构.树.BST; | ||
|
||
import 数据结构.树.TreeNode; | ||
|
||
/** | ||
* Created by 周杰伦 on 2018/4/20. | ||
*/ | ||
public class 从有序数组中构造二叉查找树 { | ||
// Leetcode : 108. Convert Sorted Array to Binary Search Tree (Easy) | ||
|
||
public TreeNode sortedArrayToBST(int[] nums) { | ||
return toBST(nums, 0, nums.length - 1); | ||
} | ||
|
||
//根据节点的位置关系进行递归 | ||
private TreeNode toBST(int[] nums, int sIdx, int eIdx){ | ||
if(sIdx > eIdx) return null; | ||
int mIdx = (sIdx + eIdx) / 2; | ||
TreeNode root = new TreeNode(nums[mIdx]); | ||
root.left = toBST(nums, sIdx, mIdx - 1); | ||
root.right = toBST(nums, mIdx + 1, eIdx); | ||
return root; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package 数据结构.树.BST; | ||
|
||
import 数据结构.树.TreeNode; | ||
|
||
/** | ||
* Created by 周杰伦 on 2018/4/20. | ||
*/ | ||
public class 修剪二叉查找树 { | ||
// 二叉查找树(BST):根节点大于等于左子树所有节点,小于等于右子树所有节点。 | ||
// | ||
// 只保留值在 L ~ R 之间的节点 | ||
|
||
//这个解法绝了。 | ||
public TreeNode trimBST(TreeNode root, int L, int R) { | ||
if(root == null) return null; | ||
//若根节点比下限小。只保留右边。 | ||
//若根节点比上限大,只保留左边。 | ||
if(root.val > R) return trimBST(root.left, L, R); | ||
if(root.val < L) return trimBST(root.right, L, R); | ||
//如果根节点在中间,那么左子树也进行以上操作进行保留 | ||
root.left = trimBST(root.left, L, R); | ||
root.right = trimBST(root.right, L, R); | ||
// | ||
return root; | ||
} | ||
// public TreeNode trimBST(TreeNode root, int L, int R) { | ||
// if (root == null)return null; | ||
// TreeNode t = new TreeNode(root.val); | ||
// trimBST(root.left,L,R); | ||
// if (root.val < L) { | ||
// root = root.right; | ||
// | ||
// }else if (root.val > R) { | ||
// root = root.left; | ||
// } | ||
// trimBST(root.right,L,R); | ||
// return root; | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
package 数据结构.树; | ||
package 数据结构.树.递归; | ||
|
||
import 数据结构.树.TreeNode; | ||
|
||
/** | ||
* Created by 周杰伦 on 2018/4/20. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package 数据结构.树.递归.树的操作; | ||
|
||
import 数据结构.树.TreeNode; | ||
|
||
/** | ||
* Created by 周杰伦 on 2018/4/20. | ||
* | ||
3 | ||
/ \ | ||
4 5 | ||
/ \ | ||
1 2 | ||
4 | ||
/ \ | ||
1 2 | ||
*/ | ||
public class 子树 { | ||
//判断是非问题时,先把不满足的情况找出来。再把需要满足的情况找出来。最后返回递归 | ||
public boolean isSubtree(TreeNode s, TreeNode t) { | ||
if (s == null)return false; | ||
if (recur(s,t))return true; | ||
if (isSubtree(s.left,t)) return true; | ||
if (isSubtree(s.right,t)) return true; | ||
return false; | ||
} | ||
|
||
public boolean recur (TreeNode s, TreeNode t) { | ||
if (s == null && t == null)return true; | ||
if (s == null || t == null)return false; | ||
if (s.val != t.val)return false; | ||
return recur(s.left, t.left) && recur(s.right, t.right); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
package 数据结构.树; | ||
package 数据结构.树.递归.树的操作; | ||
|
||
import 数据结构.树.TreeNode; | ||
|
||
/** | ||
* Created by 周杰伦 on 2018/4/20. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
package 数据结构.树; | ||
package 数据结构.树.递归.树的操作; | ||
|
||
import 数据结构.树.TreeNode; | ||
|
||
/** | ||
* Created by 周杰伦 on 2018/4/20. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
package 数据结构.树; | ||
package 数据结构.树.递归.树的操作; | ||
|
||
import 数据结构.树.TreeNode; | ||
|
||
/** | ||
* Created by 周杰伦 on 2018/4/20. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
package 数据结构.树; | ||
package 数据结构.树.递归; | ||
|
||
import 数据结构.树.TreeNode; | ||
|
||
/** | ||
* Created by 周杰伦 on 2018/4/13. | ||
|
2 changes: 1 addition & 1 deletion
2
src/数据结构/树/二叉查找树的最近公共祖先.java → src/数据结构/树/递归/节点/二叉查找树的最近公共祖先.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package 数据结构.树; | ||
package 数据结构.树.递归.节点; | ||
|
||
/** | ||
* Created by 周杰伦 on 2018/4/20. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package 数据结构.树; | ||
package 数据结构.树.递归.节点; | ||
|
||
/** | ||
* Created by 周杰伦 on 2018/4/20. | ||
|
2 changes: 1 addition & 1 deletion
2
src/数据结构/树/找出二叉树中第二小的节点.java → src/数据结构/树/递归/节点/找出二叉树中第二小的节点.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package 数据结构.树; | ||
package 数据结构.树.递归.节点; | ||
|
||
/** | ||
* Created by 周杰伦 on 2018/4/20. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package 数据结构.树.递归.节点; | ||
|
||
import 数据结构.树.TreeNode; | ||
|
||
/** | ||
* Created by 周杰伦 on 2018/4/20. | ||
*/ | ||
public class 统计左叶子节点的和 { | ||
public int sumOfLeftLeaves(TreeNode root) { | ||
if (root == null)return 0; | ||
int sum = 0; | ||
if (root.left != null && root.left.left == null && root.left.right == null) { | ||
sum += root.left.val; | ||
} | ||
sum += sumOfLeftLeaves(root.left); | ||
sum += sumOfLeftLeaves(root.right); | ||
return sum; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package 数据结构.树.递归.路径; | ||
|
||
import 数据结构.树.TreeNode; | ||
|
||
/** | ||
* Created by 周杰伦 on 2018/4/20. | ||
*/ | ||
public class 两节点的最长路径 { | ||
//从任一个节点出发找两边最长的边,相加即为该点为中心的最长路径。 | ||
//遍历一遍二叉树找出这些路径中最长的。 | ||
public int diameterOfBinaryTree(TreeNode root) { | ||
if (root == null)return 0; | ||
int left = recur(root.left, 0); | ||
int right = recur(root.right, 0); | ||
int max = left + right; | ||
max = Math.max(max, diameterOfBinaryTree(root.left)); | ||
max = Math.max(max, diameterOfBinaryTree(root.right)); | ||
return max; | ||
} | ||
public int recur (TreeNode root, int sum) { | ||
if (root == null)return sum; | ||
if (root.left == null && root.right == null) return sum + 1; | ||
int left = recur(root.left, sum + 1); | ||
int right = recur(root.right, sum + 1); | ||
return Math.max(left, right); | ||
} | ||
|
||
} |
4 changes: 3 additions & 1 deletion
4
src/数据结构/树/判断路径和是否等于一个数.java → src/数据结构/树/递归/路径/判断路径和是否等于一个数.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
package 数据结构.树; | ||
package 数据结构.树.递归.路径; | ||
|
||
import 数据结构.树.TreeNode; | ||
|
||
/** | ||
* Created by 周杰伦 on 2018/4/20. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package 数据结构.树.递归.路径; | ||
|
||
import 数据结构.树.TreeNode; | ||
|
||
/** | ||
* Created by 周杰伦 on 2018/4/20. | ||
*/ | ||
public class 最小路径 { | ||
//其实找的是根节点到叶子节点的最小路径值 | ||
public int minDepth(TreeNode root) { | ||
if (root == null)return 0; | ||
int left = minDepth(root.left); | ||
int right = minDepth(root.right); | ||
//如果左右子树有一个为空,只能取不为空的长度 | ||
if (left == 0 || right == 0)return left + right + 1; | ||
//如果都不为空,则取小的那一个。妙啊 | ||
return Math.min(left,right) + 1; | ||
|
||
} | ||
|
||
|
||
} |
2 changes: 1 addition & 1 deletion
2
src/数据结构/树/相同节点值的最大路径长度.java → src/数据结构/树/递归/路径/相同节点值的最大路径长度.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package 数据结构.树; | ||
package 数据结构.树.递归.路径; | ||
|
||
/** | ||
* Created by 周杰伦 on 2018/4/20. | ||
|
4 changes: 3 additions & 1 deletion
4
src/数据结构/树/统计路径和等于一个数的路径数量.java → src/数据结构/树/递归/路径/统计路径和等于一个数的路径数量.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
package 数据结构.树; | ||
package 数据结构.树.递归.路径; | ||
|
||
import 数据结构.树.TreeNode; | ||
|
||
/** | ||
* Created by 周杰伦 on 2018/4/20. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package 数据结构.树; | ||
package 数据结构.树.遍历; | ||
|
||
/** | ||
* Created by 周杰伦 on 2018/4/20. | ||
|