目录
1、513.找树左下角的值
层次遍历,easy
class Solution {
public int findBottomLeftValue(TreeNode root) {
Queue<TreeNode> que = new LinkedList<>();
que.add(root);
int res = root.val;
while(!que.isEmpty())
{
int size = que.size();
for (int i = 0; i < size; ++i)
{
TreeNode tmp = que.poll();
if(tmp.left != null) que.add(tmp.left);
if(tmp.right != null) que.add(tmp.right);
if(i == 0) res = tmp.val;
}
}
return res;
}
}
2、112. 路径总和
递归
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
if(root == null) return false;
targetSum = targetSum - root.val;
if(root.left == null && root.right == null) return targetSum == 0;
boolean flag1 = false, flag2 = false;
if(root.left != null) flag1 = hasPathSum(root.left,targetSum);
if(flag1) return true;
if(root.right != null) flag2 = hasPathSum(root.right,targetSum);
return flag2;
}
}
3、113. 路径总和ii
递归
class Solution {
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
List<List<Integer>> res = new ArrayList<>();
if(root == null) return res;
List<Integer> pathList = new ArrayList<>();
getPath(root,pathList,res,targetSum);
return res;
}
public void getPath(TreeNode root,List<Integer> pathList,List<List<Integer>> res,int sum)
{
pathList.add(root.val);
sum = sum - root.val;
if(root.left == null && root.right == null)
{
if(sum == 0)
res.add(new ArrayList<>(pathList));
return;
}
if(root.left != null)
{
getPath(root.left,pathList,res,sum);
pathList.remove(pathList.size() - 1);
}
if(root.right != null)
{
getPath(root.right,pathList,res,sum);
pathList.remove(pathList.size() - 1);
}
}
}
4、106.从中序与后序遍历序列构造二叉树
递归
class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
int len = inorder.length;
if (len == 1) return new TreeNode(inorder[0]);
int root = postorder[len - 1];
int root_index = 0;
for(int i = 0; i < len; ++i)
{
if(inorder[i] == root) root_index = i;
}
TreeNode res = new TreeNode(root);
if(root_index != 0) res.left = buildTree(Arrays.copyOfRange(inorder,0,root_index),Arrays.copyOfRange(postorder,0,root_index));
if(root_index != len - 1) res.right = buildTree(Arrays.copyOfRange(inorder,root_index + 1,len),Arrays.copyOfRange(postorder,root_index,len - 1));
return res;
}
}
5、105. 从前序与中序遍历序列构造二叉树
递归
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
int len = inorder.length;
if (len == 1) return new TreeNode(inorder[0]);
int root = preorder[0];
int root_index = 0;
for(int i = 0; i < len; ++i)
{
if(inorder[i] == root) root_index = i;
}
TreeNode res = new TreeNode(root);
if(root_index != 0) res.left = buildTree(Arrays.copyOfRange(preorder,1,root_index + 1),Arrays.copyOfRange(inorder,0,root_index));
if(root_index != len - 1) res.right = buildTree(Arrays.copyOfRange(preorder,root_index + 1,len),Arrays.copyOfRange(inorder,root_index + 1,len));
return res;
}
}
6、654. 最大二叉树
递归
class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
int len = nums.length;
if(len == 1) return new TreeNode(nums[0]);
int root = nums[0];
int root_index = 0;
for(int i = 0; i < len; ++i)
{
if (nums[i] > root)
{
root = nums[i];
root_index = i;
}
}
TreeNode res = new TreeNode(root);
if(root_index != 0) res.left = constructMaximumBinaryTree(Arrays.copyOfRange(nums,0,root_index));
if(root_index != len - 1) res.right = constructMaximumBinaryTree(Arrays.copyOfRange(nums,root_index + 1,len));
return res;
}
}
7、617. 合并二叉树
递归
class Solution {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if(root1 == null) return root2;
if(root2 == null) return root1;
root1.val += root2.val;
if(root1.left != null || root2.left != null) root1.left = mergeTrees(root1.left,root2.left);
if(root1.right != null || root2.right != null) root1.right = mergeTrees(root1.right,root2.right);
return root1;
}
}