LeetCode513.找树左下角的值
public int findBottomLeftValue(TreeNode root) {
if(root==null) {
return 0;
}
LinkedList<TreeNode> queue = new LinkedList<>();
int k=0;
//放入头节点
queue.add(root);
//遍历
while(!queue.isEmpty()) {
int size = queue.size();
for(int i=0;i<size;i++) {
TreeNode node = queue.remove();
if(i==0) {
k=node.val;
}
if(node.left!=null) {
queue.add(node.left);
}
if(node.right!=null) {
queue.add(node.right);
}
}
}
return k;
}
LeetCode112.路径总和
public boolean hasPathSum(TreeNode root, int targetSum) {
if(root==null) {
return false;
}
return dfs(root,root.val,targetSum);
}
public boolean dfs(TreeNode root,int cur,int targetSum) {
//叶子节点
if(root.left==null&root.right==null) {
//判断targetSum是否为0
if(targetSum==cur) {
return true;
}
return false;
}
boolean dfs1=false,dfs2=false;
//左节点
if(root.left!=null) {
dfs1 = dfs(root.left,cur+root.left.val,targetSum);
}
//右节点
if(root.right!=null) {
dfs2 = dfs(root.right,cur+root.right.val,targetSum);
}
return dfs1 || dfs2;
}
LeetCode113.路径总和II
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
if(root==null) {
return res;
}
path.add(root.val);
dfs2(root,root.val,targetSum);
return res;
}
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public void dfs2(TreeNode root,int cur,int targetSum) {
//叶子节点
if(root.left==null&root.right==null) {
//判断targetSum是否为cur
if(targetSum==cur) {
//指向同一个地址,所以每次需要重新备份一份path
res.add(new ArrayList<>(path));
}
return;
}
//左节点
if(root.left!=null) {
path.add(root.left.val);
dfs2(root.left,cur+root.left.val,targetSum);
path.remove(path.size()-1);
}
//右节点
if(root.right!=null) {
path.add(root.right.val);
dfs2(root.right,cur+root.right.val,targetSum);
path.remove(path.size()-1);
}
}
LeetCode106.从中序与后序遍历序列构造二叉树
public TreeNode buildTree(int[] inorder, int[] postorder) {
return buildHelper(inorder,postorder,0,inorder.length,0,postorder.length);
}
public TreeNode buildHelper(int[] inorder,int[]postorder,int inStart,int inEnd,int postStart,int postEnd) {
//递归结束条件
if(postStart==postEnd) return null;
//找根节点
TreeNode root = new TreeNode(postorder[postEnd-1]);
//判断是否是叶子节点
if(postEnd-postStart==1) return root;
//切割中序数组
int mid;
for(mid=inStart;mid<inEnd;mid++) {
if(inorder[mid]==root.val) {
break;
}
}
//切割中序和后序数组
int left = postStart+(mid-inStart);
//递归求解
root.left=buildHelper(inorder,postorder,inStart,mid,postStart,left);
root.right=buildHelper(inorder,postorder,mid+1,inEnd,left,postEnd-1);
return root;
}
LeetCode106.从中序与后序遍历序列构造二叉树
public TreeNode buildTree(int[] preorder, int[] inorder) {
return builderHelper(preorder,inorder,0,preorder.length,0,inorder.length);
}
public TreeNode builderHelper(int[] preorder,int[]inorder,int preStart,int preEnd,int inStart,int inEnd) {
//递归结束条件
if(preStart==preEnd) return null;
//找根节点
TreeNode root = new TreeNode(preorder[preStart]);
//判断是否是叶子节点
if(preEnd-preStart==1) return root;
//切割中序数组
int mid;
for(mid=inStart;mid<inEnd;mid++) {
if(inorder[mid]==root.val) {
break;
}
}
//切割中序和后序数组
int leftStart=preStart+1;
int leftEnd=leftStart+(mid-inStart);
//递归求解
root.left=builderHelper(preorder,inorder,leftStart,leftEnd,inStart,mid);
root.right=builderHelper(preorder,inorder,leftEnd,preEnd,mid+1,inEnd);
return root;
}