代码随想录[原文指路]
1.左叶子之和[404]
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if(root==null) return 0;
int sum=0;
if(root.left!=null&&root.left.left==null&&root.left.right==null) {
sum += root.left.val;
}
int leftsum = sumOfLeftLeaves(root.left);
int rightsum = sumOfLeftLeaves(root.right);
return leftsum+rightsum+sum;
}
}
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if(root==null) return 0;
int sum=0;
Stack<TreeNode> stack = new Stack();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node = stack.pop();
if(node.left!=null && node.left.left==null && node.left.right==null){
sum += node.left.val;
}
if(node.left!=null) stack.push(node.left);
if(node.right!=null) stack.push(node.right);
}
return sum;
}
}
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if(root==null) return 0;
int sum=0;
Queue<TreeNode> queue = new LinkedList();
queue.offer(root);
while(!queue.isEmpty()){
int size = queue.size();
while(size-->0){
TreeNode node = queue.poll();
if(node.left!=null){
if(node.left.left==null&&node.left.right==null){
sum += node.left.val;
}else{
queue.offer(node.left);
}
}
if(node.right!=null) queue.offer(node.right);
}
}
return sum;
}
}
2.找树左下角的值[513]
class Solution {
private int maxheight = -1;
private int value = 0;
public int findBottomLeftValue(TreeNode root) {
if(root==null) return 0;
findLeftValue(root,0);
return value;
}
private void findLeftValue(TreeNode root,int deep){
if(root.left==null && root.right==null) {
if(deep>maxheight){
value=root.val;
maxheight=deep;
}
}
if(root.left!=null) findLeftValue(root.left,deep+1);
if(root.right!=null) findLeftValue(root.right,deep+1);
}
}
class Solution {
public int findBottomLeftValue(TreeNode root) {
if(root==null) return 0;
int leftOrder = 0;
Queue<TreeNode> queue = new LinkedList();
queue.offer(root);
while(!queue.isEmpty()){
int size = queue.size();
leftOrder = queue.peek().val;
while(size-->0){
TreeNode node = queue.poll();
if(node.left!=null) queue.offer(node.left);
if(node.right!=null) queue.offer(node.right);
}
}
return leftOrder;
}
}
3.路径总和[112]
1.如果需要搜索整棵二叉树且不用处理递归返回值,递归函数就不要返回值
2.如果需要搜索整棵二叉树且需要处理递归返回值,递归函数就需要返回值
3.如果要搜索其中一条符合条件的路径,那么递归一定需要返回值,因为遇到符合条件的路径了就要及时返回
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
if(root==null) return false;
targetSum -= root.val;
if(targetSum==0&&root.left==null&&root.right==null) return true;
boolean bool1 = hasPathSum(root.left,targetSum);
boolean bool2 = hasPathSum(root.right,targetSum);
return bool1||bool2;
}
}
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
if(root==null) return false;
Stack<TreeNode> stack1 = new Stack();
Stack<Integer> stack2 = new Stack();
stack1.push(root);
stack2.push(root.val);
while(!stack1.isEmpty()){
int size = stack1.size();
while(size-->0){
TreeNode node = stack1.pop();
int sum=stack2.pop();
if(node.left==null && node.right==null && sum==targetSum)return true;
if(node.left!=null){
stack1.push(node.left);
stack2.push(sum+node.left.val);
}
if(node.right!=null){
stack1.push(node.right);
stack2.push(sum+node.right.val);
}
}
}
return false;
}
}
4.路径总和 II[113]
class Solution {
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
List<List<Integer>> ress = new ArrayList();
if(root==null) return ress;
List<Integer> res = new ArrayList();
getpathSum(root,targetSum,res,ress);
return ress;
}
public void getpathSum(TreeNode root, int targetSum,List<Integer> res,List<List<Integer>> ress) {
res.add(root.val);
targetSum -= root.val;
if(root.left==null && root.right==null){
if(targetSum==0) ress.add(new ArrayList<>(res));
return ;
}
if(root.left!=null){
getpathSum(root.left,targetSum,res,ress);
res.remove(res.size()-1);
}
if(root.right!=null){
getpathSum(root.right,targetSum,res,ress);
res.remove(res.size()-1);
}
}
}
5.从中序与后序遍历序列构造二叉树[106]
class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
return treeBuild(inorder,0,inorder.length,postorder,0,postorder.length);
}
public TreeNode treeBuild(int[] inorder,int inleft,int inright,int[] postorder,int postleft,int postright){
if(inright-inleft<1) return null;
if(inright-inleft==1) return new TreeNode(inorder[inleft]);
int rootVal = postorder[postright-1];
TreeNode root = new TreeNode(rootVal);
int rootIndex = 0;
for(int i=inleft;i<inright;i++){
if(inorder[i]==rootVal){
rootIndex=i;
break;
}
}
root.left = treeBuild(inorder,inleft,rootIndex,postorder,postleft,postleft+(rootIndex-inleft));
root.right = treeBuild(inorder,rootIndex+1,inright,postorder,postleft+(rootIndex-inleft),postright-1);
return root;
}
}
6.从前序与中序遍历序列构造二叉树[105]
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
return treeBuild(inorder,0,inorder.length,preorder,0,preorder.length);
}
public TreeNode treeBuild(int[] inorder,int inleft,int inright,int[] preorder,int preleft,int preright){
if(inright-inleft<1) return null;
if(inright-inleft==1) return new TreeNode(inorder[inleft]);
int rootVal = preorder[preleft];
TreeNode root = new TreeNode(rootVal);
int rootIndex = 0;
for(int i=inleft;i<inright;i++){
if(inorder[i]==rootVal){
rootIndex=i;
break;
}
}
root.left = treeBuild(inorder,inleft,rootIndex,preorder,preleft+1,preleft+(rootIndex-inleft));
root.right = treeBuild(inorder,rootIndex+1,inright,preorder,preleft+(rootIndex-inleft)+1,preright);
return root;
}
}
7.最大二叉树[654]
class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
return buildMaxTree(nums,0,nums.length);
}
public static TreeNode buildMaxTree(int[] nums,int leftIndex,int rightIndex){
if(rightIndex-leftIndex<1) return null;
if(rightIndex-leftIndex==1) return new TreeNode(nums[leftIndex]);
int maxIndex = -1;
int maxVal = 0;
for(int i=leftIndex;i<rightIndex;i++){
if(nums[i]>maxVal){
maxIndex=i;
maxVal=nums[i];
}
}
TreeNode root = new TreeNode(maxVal);
root.left=buildMaxTree(nums,leftIndex,maxIndex);
root.right=buildMaxTree(nums,maxIndex+1,rightIndex);
return root;
}
}
8.合并二叉树[617]
class Solution {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if(root1==null) return root2;
if(root2==null) return root1;
TreeNode root = new TreeNode(root1.val+root2.val);
root.left = mergeTrees(root1.left,root2.left);
root.right = mergeTrees(root1.right,root2.right);
return root;
}
}
class Solution {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if(root1==null) return root2;
if(root2==null) return root1;
root1.val = root1.val+root2.val;
root1.left = mergeTrees(root1.left,root2.left);
root1.right = mergeTrees(root1.right,root2.right);
return root1;
}
}