题目一:
求一个二叉树的最小深度。
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes aong the shortest path from the root node down to the neartest leaf node.
TreeNode类:
public class TreeNode<T> { public T val; public TreeNode<T> left = null; public TreeNode<T> right = null; public TreeNode<T> parent = null; public TreeNode(T val) { this.val = val; } }
MinDepth类:
1 public class MinDepth { 2 public int run(TreeNode root) { 3 if (root == null) 4 return 0; 5 int depLeft = run(root.left); 6 int depRight = run(root.right); 7 if (depLeft == 0 || depRight == 0) 8 return 1 + depLeft + depRight; 9 return 1 + Math.min(depLeft, depRight); 10 } 11 }
题目二:
路径数字串之和。
Given a binary containing digits from 0-9 only, each root-to-leaf path path could represent a number. An example is the root-to-leaf path1->2->3which represents the number123 Find the total sum of all root-to-leaf numbers. For example, 1 2 3 The root-to-leaf path1->2represents the numbe 12. The root-to-leaf path1->3represents the number13. Return the sum = 12 + 13 = 25
思路:题目的意思大概是,从根节点到叶子节点的路径中,把其中的路径上的数字拼接成一个字符串形成一个数字,需要把这些所有路径上的字符串数字加起来。使用深搜的办法即可轻松解决,递归搜索左子树与右子树,当搜索到叶子节点的时候表示路径结束应该把当前形成的字符串加入到List中,等到所有的路径搜索完毕,遍历List将数字字符串转化成数字然后加起来即可。注意结点类为题目一的结点类。
import java.util.ArrayList; import java.util.List; import org.junit.Test; public class sum_root_leaf { public int sumNumbers(TreeNode<Integer> root) { if (root == null) return 0; f("", root); int sum = 0; for (int i = 0; i < list.size(); i++) { sum += Integer.parseInt(list.get(i)); } return sum; } List<String> list = new ArrayList<String>(); /** * 带前缀的dfs * * @param pre * @param node */ void f(String pre, TreeNode<Integer> node) { String _pre = pre + node.val;// 将当前节点的值,附加到pre上面 if (node.left == null && node.right == null) {// 当前节点是叶子,结算 list.add(_pre); return; } if (node.left != null) f(_pre, node.left); if (node.right != null) f(_pre, node.right); } @Test public void t() { sum_root_leaf obj = new sum_root_leaf(); TreeNode<Integer> root = new TreeNode<>(1); TreeNode<Integer> l = new TreeNode<>(2); TreeNode<Integer> ll = new TreeNode<>(4); TreeNode<Integer> lr = new TreeNode<>(7); TreeNode<Integer> r = new TreeNode<>(3); TreeNode<Integer> rr = new TreeNode<>(5); TreeNode<Integer> rl = new TreeNode<>(8); root.left = l; root.right = r; l.left = ll; l.right = lr; r.right = rr; r.left = rl; int sum = obj.sumNumbers(root); System.out.println(sum); } }
题目三:
使用有序数组构建高度最低的BST。
Given a sorted(increasing order) array, write an algorithm to create a binary search tree with mininal height;
思路:要想使得树的高度最小那么应该使树的两边的节点尽可能分布均匀,那么我们可以采用首尾相加相除的方式来得到当前的根节点(中间节点)是谁,取得中间节点之后之后那么以中间为界限分为了左右两个区间,对于左右两个区间我们又可以使用同样的方式来进行处理,那么这个时候我们可以使用递归的方式来进行解决即可。注意结点类为题目一的结点类。
public class BSTWithMinHeight { TreeNode createMinBst(int[] arr) { return createMinBst(arr, 0, arr.length - 1); } private TreeNode createMinBst(int[] arr, int start, int end) { if (start > end) return null; int mid = start + ((end - start) >> 1); TreeNode left = createMinBst(arr, start, mid - 1); TreeNode right = createMinBst(arr, mid + 1, end); TreeNode res = new TreeNode(arr[mid]); res.left = left; res.right = right; return res; } public static void main(String[] args) { BSTWithMinHeight obj = new BSTWithMinHeight(); int[] arr = { 1, 2, 3, 4, 5, 6, 7 }; TreeNode minBst = obj.createMinBst(arr); System.out.println(minBst); } }