Day16
LeetCode513 找树左下角的值
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
//递归法
class Solution {
private int Depth = -1;
private int value = 0;
public int findBottomLeftValue(TreeNode root) {
value = root.val;
findLeftValue(root,0);
return value;
}
private void findLeftValue (TreeNode root,int depth) {
if (root == null) return;
if (root.left == null && root.right == null) {
if (depth > Depth) {
value = root.val;
Depth = depth;
}
}
if (root.left != null) findLeftValue(root.left,depth + 1);
if (root.right != null) findLeftValue(root.right,depth + 1);
}
}
//迭代法
class Solution {
public int findBottomLeftValue(TreeNode root) {
int res = 0;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
int size = queue.size();
for(int i = 0; i < size; i++){
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;
}
}
LeetCode112 路径总和
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
//递归法|
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
if(root == null) return false;
if(root.left == null && root.right == null) return root.val == targetSum;
return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
}
}
//迭代法
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
if(root == null) return false;
Deque<TreeNode> stack = new LinkedList<>();
Deque<Integer> sumStack = new LinkedList<>();
stack.offer(root);
sumStack.push(root.val);
while(!stack.isEmpty()){
TreeNode topNode = stack.peek();
Integer topSum = sumStack.pop();
if(topNode != null){
stack.push(null);
sumStack.push(topSum);
if(topNode.right != null) {
stack.push(topNode.right);
sumStack.push(topSum + topNode.right.val);
}
if(topNode.left != null) {
stack.push(topNode.left);
sumStack.push(topSum + topNode.left.val);
}
}else{
stack.pop();
topNode = stack.pop();
if(topNode.left == null && topNode.right == null && topSum == targetSum) return true;
}
}
return false;
}
}
LeetCode113 路径总和II
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
//递归法
class Solution {
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
List<List<Integer>> res = new ArrayList<>();
if(root == null) return res;
traversal(res, new LinkedList<Integer>(), root, targetSum);
return res;
}
private void traversal(List<List<Integer>> res, List<Integer> path, TreeNode root, int targetSum){
path.add(root.val);
if(root.left == null && root.right == null && root.val == targetSum)
res.add(new ArrayList<Integer>(path));
else{
if(root.left != null){
traversal(res, path, root.left, targetSum - root.val);
path.remove(path.size() - 1);
}
if(root.right != null){
traversal(res, path, root.right, targetSum - root.val);
path.remove(path.size() - 1);
}
}
}
}
LeetCode106 从中序与后序遍历序列构造二叉树
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
Map<Integer, Integer> inorderIndex = new HashMap<>();
public TreeNode buildTree(int[] inorder, int[] postorder) {
for(int i = 0; i < inorder.length; i++) inorderIndex.put(inorder[i], i);
return buildTree(inorder, postorder, 0, inorder.length - 1, 0, postorder.length - 1);
}
private TreeNode buildTree(int[] inorder, int[] postorder, int inStart, int inEnd, int postStart, int postEnd){
if(inStart > inEnd || postStart > postEnd) return null;
int rootVal = postorder[postEnd];
int leftInEnd = inorderIndex.get(rootVal) - 1;
TreeNode root = new TreeNode(rootVal);
int diff = leftInEnd - inStart;
root.left = buildTree(inorder, postorder, inStart, leftInEnd, postStart, postStart + diff);
root.right = buildTree(inorder, postorder, leftInEnd + 2, inEnd, postStart + diff + 1, postEnd - 1);
return root;
}
}
ldTree(inorder, postorder, inStart, leftInEnd, postStart, postStart + diff);
root.right = buildTree(inorder, postorder, leftInEnd + 2, inEnd, postStart + diff + 1, postEnd - 1);
return root;
}
}

220

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



