513 这个题是要找最左边的结点,不是最后一层的第一个左节点,就很简单了
class Solution {
public int findBottomLeftValue(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
if(root != null)
queue.offer(root);
int res = 0;
while(!queue.isEmpty()){
int len = queue.size();
for(int i = 0; i<len; 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;
}
}
112
方法和求所有路径很像,不同的是,这个只要存在符合条件的,return就行了。
class Solution {
ArrayList<Integer> list = new ArrayList<Integer>();
public boolean hasPathSum(TreeNode root, int targetSum) {
if(root == null){
return false;
}
ArrayList<Integer> path = new ArrayList<>();
return deal(root,path,targetSum);
}
public boolean deal(TreeNode root,ArrayList<Integer> path,int target){
path.add(root.val);
if(root.left == null && root.right == null){
int sum = 0;
for(int i = 0;i<path.size();i++){
sum = sum + path.get(i);
}
if(sum == target){
return true;
}
}
if(root.left != null){
if(deal(root.left,path,target)){
return true;
}
path.remove(path.size() - 1);
}
if(root.right != null){
if(deal(root.right,path,target)){
return true;
}
path.remove(path.size() - 1);
}
return false;
}
}
113.
和112一样,就是注意在list加path的时候,不能简单的写成:list.add(path); 这样的话指向的还是原来的列表,需要重新建一个新的列表,用list.add(new ArrayList<>(path))
class Solution {
List<List<Integer>> list = new ArrayList<List<Integer>>();
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
List<Integer> path = new ArrayList<>();
if(root == null){
return list;
}
deal(root,path,targetSum);
return list;
}
public void deal(TreeNode root,List<Integer> path, int targetSum){
path.add(root.val);
int sum = 0;
if(root.left == null && root.right == null){
for(int i = 0; i < path.size();i++){
sum = sum + path.get(i);
}
if(sum == targetSum){
list.add(new ArrayList<>(path));
}
}
if(root.left != null){
deal(root.left,path,targetSum);
path.remove(path.size() - 1);
}
if(root.right != null){
deal(root.right,path,targetSum);
path.remove(path.size() - 1);
}
}
}
106.中序和后序构造二叉树:
class Solution {
Map<Integer,Integer> map = new HashMap<>();
public TreeNode buildTree(int[] inorder, int[] postorder) {
if(inorder == null){
return new TreeNode();
}
for(int i = 0;i<inorder.length;i++){
map.put(inorder[i],i);
}
return deal(inorder,0,inorder.length,postorder,0,postorder.length);
}
public TreeNode deal(int[] inorder, int inIndex,int inEnd,int[] postorder,int postIndex,int postEnd){
if(inIndex >= inEnd || postIndex >= postEnd){
return null;
}
int centerIndex = map.get(postorder[postEnd - 1]);
TreeNode center = new TreeNode(inorder[centerIndex]);
int lenOfLeft = centerIndex - inIndex;
center.left = deal(inorder,inIndex,centerIndex,postorder, postIndex,postIndex + lenOfLeft);
center.right = deal(inorder,centerIndex + 1, inEnd,postorder, postIndex + lenOfLeft,postEnd - 1);
return center;
}
}
105.前序和中序遍历二叉树
class Solution {
Map<Integer,Integer> map = new HashMap<>();
public TreeNode buildTree(int[] inorder, int[] postorder) {
if(inorder == null){
return new TreeNode();
}
for(int i = 0;i<inorder.length;i++){
map.put(inorder[i],i);
}
return deal(inorder,0,inorder.length,postorder,0,postorder.length);
}
public TreeNode deal(int[] inorder, int inIndex,int inEnd,int[] postorder,int postIndex,int postEnd){
if(inIndex >= inEnd || postIndex >= postEnd){
return null;
}
int centerIndex = map.get(postorder[postEnd - 1]);
TreeNode center = new TreeNode(inorder[centerIndex]);
int lenOfLeft = centerIndex - inIndex;
center.left = deal(inorder,inIndex,centerIndex,postorder, postIndex,postIndex + lenOfLeft);
center.right = deal(inorder,centerIndex + 1, inEnd,postorder, postIndex + lenOfLeft,postEnd - 1);
return center;
}
}
有几个需要注意的点:
1.map里要放的是“中”在中间的数组,所以是中序遍历的数组。因为前序遍历的“中”在开头,后序遍历的在结尾最后一个;
2.注意如果采用左闭右开的方式,那么第一轮的时候,两个数组的开始索引是1毋庸置疑,结束索引应该是length,而不是length - 1,因为是右开,不包含这个数。
3.注意前序遍历时,每轮遍历的左开始范围应该是preBegin + 1,因为preBegin是“中”;
后序遍历时,每轮遍历的右结束范围应该是postEnd - 1.