Skip to content

Commit

Permalink
树的算法。递归与二叉
Browse files Browse the repository at this point in the history
  • Loading branch information
h2pl committed Apr 22, 2018
1 parent f43326f commit 452848c
Show file tree
Hide file tree
Showing 25 changed files with 197 additions and 67 deletions.
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;
}
}
39 changes: 39 additions & 0 deletions src/数据结构/树/BST/修剪二叉查找树.java
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;
// }
}
8 changes: 4 additions & 4 deletions src/数据结构/树/TreeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* Created by 周杰伦 on 2018/4/19.
*/
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
}
7 changes: 0 additions & 7 deletions src/数据结构/树/两节点的最长路径.java

This file was deleted.

This file was deleted.

7 changes: 0 additions & 7 deletions src/数据结构/树/修剪二叉查找树.java

This file was deleted.

7 changes: 0 additions & 7 deletions src/数据结构/树/子树.java

This file was deleted.

16 changes: 0 additions & 16 deletions src/数据结构/树/最小路径.java

This file was deleted.

7 changes: 0 additions & 7 deletions src/数据结构/树/统计左叶子节点的和.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package 数据结构.;
package 数据结构..递归;

import 数据结构..TreeNode;

/**
* Created by 周杰伦 on 2018/4/20.
Expand Down
35 changes: 35 additions & 0 deletions src/数据结构/树/递归/树的操作/子树.java
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);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package 数据结构.;
package 数据结构..递归.树的操作;

import 数据结构..TreeNode;

/**
* Created by 周杰伦 on 2018/4/20.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package 数据结构.;
package 数据结构..递归.树的操作;

import 数据结构..TreeNode;

/**
* Created by 周杰伦 on 2018/4/20.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package 数据结构.;
package 数据结构..递归.树的操作;

import 数据结构..TreeNode;

/**
* Created by 周杰伦 on 2018/4/20.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package 数据结构.;
package 数据结构..递归;

import 数据结构..TreeNode;

/**
* Created by 周杰伦 on 2018/4/13.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package 数据结构.;
package 数据结构..递归.节点;

/**
* Created by 周杰伦 on 2018/4/20.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package 数据结构.;
package 数据结构..递归.节点;

/**
* Created by 周杰伦 on 2018/4/20.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package 数据结构.;
package 数据结构..递归.节点;

/**
* Created by 周杰伦 on 2018/4/20.
Expand Down
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;
}
}
28 changes: 28 additions & 0 deletions src/数据结构/树/递归/路径/两节点的最长路径.java
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);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package 数据结构.;
package 数据结构..递归.路径;

import 数据结构..TreeNode;

/**
* Created by 周杰伦 on 2018/4/20.
Expand Down
22 changes: 22 additions & 0 deletions src/数据结构/树/递归/路径/最小路径.java
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;

}


}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package 数据结构.;
package 数据结构..递归.路径;

/**
* Created by 周杰伦 on 2018/4/20.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package 数据结构.;
package 数据结构..递归.路径;

import 数据结构..TreeNode;

/**
* Created by 周杰伦 on 2018/4/20.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package 数据结构.;
package 数据结构..遍历;

/**
* Created by 周杰伦 on 2018/4/20.
Expand Down

0 comments on commit 452848c

Please sign in to comment.