先来几个推荐的参考博客链接:
LintCode中二叉树与分治法那章有这么些题目:
要求等于target从根节点到叶子节点的路径和的组合,用DFS。
递归需要注意的是,终止情况是什么,也就是遇到叶子节点的时候返回,如果遇到叶子节点的时候恰好满足路径和等于target,就把这条路径加到总结果里面。
然后就是递归的主题,遇到左子节点、遇到右子节点的时候进入子递归。然后记得加入path路径后要remove掉,以方便保持当前路径的唯一性。
public List<List<Integer>> binaryTreePathSum(TreeNode root, int target) {
List<List<Integer>> res = new ArrayList();
if (root == null) {
return res;
}
ArrayList<Integer> path = new ArrayList<Integer>();
path.add(root.val);
dfs(res, path, root, target, root.val);
return res;
}
private void dfs(List<List<Integer>> res, ArrayList<Integer> path, TreeNode root, int target, int sum) {
// 遇到叶子节点的返回条件
if (root.left == null && root.right == null) {
if (sum == target) {
res.add(new ArrayList<Integer>(path));
}
return;
}
if (root.left != null) {
path.add(root.left.val);
dfs(res, path, root.left, target, sum + root.left.val);
path.remove(path.size() - 1);
}
if (root.right != null) {
path.add(root.right.val);
dfs(res, path, root.right, target, sum + root.right.val);
path.remove(path.size() - 1);
}
}
469. Identical Binary Tree
判断两个二叉树是否是完全一样的,连结构也要一样。很明显考的是递归。既然是递归,就注意终结条件,
自然是对不同情况的判断,参数就2个,2个树的root。
1)2个root皆空,则是identical的
2)2个root只有一个空,则非identical
3)2个root皆不空,并且root值不等,则非identical
4)2个root皆不空,并且root值相等,则进行子递归判断左子树和右子树
public boolean isIdentical(TreeNode a, TreeNode b) {
if (a == null && b == null) {
return true;
}
if (a == null || b == null) {
return false;
}
if (a.val != b.val) {
return false;
}
return isIdentical(a.left, b.left) && isIdentical(a.right, b.right);
}
470. Tweaked Identical Binary Tree
也是判断2个二叉树是否完全一样,但是与上道题不同之处在于允许了对称扭曲。同样也是分4种情况判断:
public boolean isTweakedIdentical(TreeNode a, TreeNode b) {
if (a == null && b == null) {
return true;
}
if (a == null || b == null) {
return false;
}
if (a.val == b.val) {
return (isTweakedIdentical(a.left, b.right) && isTweakedIdentical(b.left, a.right)) || (isTweakedIdentical(a.left, b.left) && isTweakedIdentical(b.right, a.right));
}
return false;
}
468. Symmetric Binary Tree
判断一颗二叉树是不是对称的,跟上道题很像,只要把这颗树的左右子节点作为参数传进去就好。
public boolean isSymmetric(TreeNode root) {
if (root == null) {
return true;
}
if (root.left == null && root.right == null) {
return true;
}
return helper(root.left, root.right);
}
public boolean helper(TreeNode a, TreeNode b) {
if (a == null && b == null) {
return true;
}
if (a == null || b == null) {
return false;
}
if (a.val == b.val) {
return (helper(a.left, b.right) && helper(b.left, a.right));
}
return false;
}
467. Complete Binary Tree
判断一棵树是不是完全二叉树。一个思路就是层次遍历,把每层的节点从左向右依此加入Stack,然后把Stack上层的None弹出,最后检查如果Stack中还有None说明不是Complete Tree 比如上面的不完全二叉树生成的数组为[1, 2, 3, None, 4, None, None],将右侧None弹出后为[1, 2, 3, None, 4],循环查找,发现还有None存在,所以是不完全二叉
public boolean isComplete(TreeNode root) {
if (root == null) {
return true;
}
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.offer(root);
boolean isNull = false;
while (!q.isEmpty()) {
TreeNode node = q.poll();
if (node.left != null) {
if (isNull == true) {
return false;
}
q.offer(node.left);
} else {
isNull = true;
}
if (node.right != null) {
if (isNull == true) {
return false;
}
q.offer(node.right);
} else {
isNull = true;
}
}
return true;
}
还有另外一种方法是递归,比较难理解,可以参考这篇博文:http://www.geeksforgeeks.org/check-whether-binary-tree-complete-not-set-2-recursive-solution/
或者参考这篇博客:http://stackoverflow.com/questions/1442674/how-to-determine-whether-a-binary-tree-is-complete
97. Maximum Depth of Binary Tree
求一个二叉树的最大深度。
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
155. Minimum Depth of Binary Tree
求二叉树的最小深度,最小深度,就是从根节点到叶子节点最近的一条路的长度。不递归的话,可以用BFS层级遍历来做。
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
}
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.offer(root);
int depth = 0;
while (!q.isEmpty()) {
int length = q.size();
depth++;
for (int i = 0; i < length; i++) {
TreeNode n = q.poll();
if (n.left == null && n.right == null) {
return depth;
}
if (n.left != null) {
q.offer(n.left);
}
if (n.right != null) {
q.offer(n.right);
}
}
}
return depth;
}也可以用递归来做,当当前节点是叶子节点时就返回结果。
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
return 1;
}
int l = (root.left == null) ? Integer.MAX_VALUE : minDepth(

本文总结了LintCode上关于二叉树和分治法的题目,包括二叉树路径求和、判断两个二叉树是否相同、对称、完全等问题。通过递归和分治策略解决各种树形结构问题,如判断平衡二叉树、最近公共祖先等。还探讨了二叉树的序列化和反序列化,以及最大路径和等挑战。
最低0.47元/天 解锁文章
971

被折叠的 条评论
为什么被折叠?



