LeetCode513 找树左下角的值
题目链接:https://programmercarl.com/0513.%E6%89%BE%E6%A0%91%E5%B7%A6%E4%B8%8B%E8%A7%92%E7%9A%84%E5%80%BC.html代码:
public class code513 {
int Deep = -1;
int res = 0;
// 迭代法(层序遍历)
public int findBottomLeftValue(TreeNode root) {
Queue<TreeNode> queue = new LinkedList();
int res = 0;
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
// 如果i=0,说明当前node为这一层的最左侧节点
TreeNode node = queue.poll();
if (i == 0) res = node.val;
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
}
return res;
}
// 递归法
public int findBottomLeftValue1(TreeNode root) {
res = root.val;
findLeftValue(root, 0);
return res;
}
private void findLeftValue(TreeNode root, int deep) {
if (root == null) return;
if (root.left == null && root.right == null) {
if (deep > Deep) {
res = root.val;
Deep = deep;
}
}
if (root.left != null) findLeftValue(root.left, deep + 1); // 左
if (root.right != null) findLeftValue(root.right, deep + 1); // 右
}
}
两种方法迭代和递归,迭代很好理解,层序遍历,遍历到最后一层找到第一个就可以,递归法注意回溯过程
LeetCode112 路径总和
题目链接:https://programmercarl.com/0112.%E8%B7%AF%E5%BE%84%E6%80%BB%E5%92%8C.html代码:
public class code112 {
public boolean hasPathSum(TreeNode root, int targetSum) {
if (root == null) return false;
// 遇到叶子节点,判断是否等于target,因为target是一层层减下去的
if (root.left == null && root.right == null) {
return root.val == targetSum;
}
if (root.left != null) {
boolean left = hasPathSum(root.left, targetSum - root.val); // 注意理解回溯过程
if (left) {
return true;
}
}
if (root.right != null) {
boolean right = hasPathSum(root.right, targetSum - root.val);
if (right) {
return true;
}
}
return false;
}
}
113
代码:
public class code113 {
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) return res;
List<Integer> path = new LinkedList<>();
findPath(root, targetSum, res, path);
return res;
}
private void findPath(TreeNode root, int targetSum, List<List<Integer>> res, List<Integer> path) {
path.add(root.val);
if (root.right == null && root.left == null) {
if (targetSum - root.val == 0) {
res.add(new ArrayList<>(path));
}
return;
}
if (root.left != null) {
findPath(root.left, targetSum - root.val, res, path);
path.remove(path.size() - 1);
}
if (root.right != null) {
findPath(root.right, targetSum - root.val, res, path);
path.remove(path.size() - 1);
}
}
}
注意比较两道题不同,一个需要返回值,一个不需要
LeetCode106 从中序与后序遍历序列构造二叉树
public class code106 {
Map<Integer, Integer> map = new HashMap<>();
public TreeNode buildTree(int[] inorder, int[] postorder) {
for (int i = 0; i < inorder.length; i++) {
map.put(inorder[i], i); // 把中序遍历中对应的数值以及位置保存到map中
}
TreeNode root = findNode(inorder, 0, inorder.length, postorder, 0, postorder.length); // 左闭右开
return root;
}
private TreeNode findNode(int[] inorder, int inBegin, int inEnd, int[] postorder, int postBegin, int postEnd) {
// 参数范围都是左闭右开
if (inBegin >= inEnd || postBegin >= postEnd) {
return null;
}
// 找到根节点在中序遍历的位置
int rootIndex = map.get(postorder[postEnd - 1]);
TreeNode root = new TreeNode(inorder[rootIndex]);
int len = rootIndex - inBegin; // 保存中序左子树的个数,用来分割后序序列
root.left = findNode(inorder, inBegin, rootIndex, postorder, postBegin, postBegin + len);
root.right = findNode(inorder, rootIndex + 1, inEnd, postorder, postBegin + len, postEnd - 1);
return root;
}
}