102.二叉树的层序遍历
/**
* 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>> levelOrder(TreeNode root) {
List<List<Integer>> list=new ArrayList<>();
if(root==null)return list;
Queue<TreeNode> tree=new LinkedList<>();
tree.add(root);
while(!tree.isEmpty()) {
int size=tree.size();
List<Integer> num=new ArrayList<>();
for(;size>0;size--) {
TreeNode fath=tree.poll();
num.add(fath.val);
if(fath.left!=null)tree.add(fath.left);
if(fath.right!=null)tree.add(fath.right);
}
list.add(num);
}
return list;
}
}
226.翻转二叉树
/**
* 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 TreeNode invertTree(TreeNode root) {
if(root==null)return root;
Queue<TreeNode> tree=new LinkedList<>();
tree.add(root);
while(!tree.isEmpty()) {
List<Integer> num=new ArrayList<>();
TreeNode fath=tree.poll();
TreeNode s=fath.left;
fath.left=fath.right;
fath.right=s;
if(fath.left!=null)tree.add(fath.left);
if(fath.right!=null)tree.add(fath.right);
}
return root;
}
}
101. 对称二叉树
/**
* 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 isSymmetric(TreeNode root) {
if(root==null)return true;
return isSym(root.left,root.right);
}
public boolean isSym(TreeNode root1,TreeNode root2) {
if(root1==null&&root2==null)return true;
else if(root1==null||root2==null)return false;
if(root1.val!=root2.val)return false;
return isSym(root1.left,root2.right)&&isSym(root1.right,root2.left);
}
}
记录
又学了一天,已经过了四分之一了,目前感觉良好,希望学完自己能蜕变一下,开学的比赛也不求什么名次至少能让会做的题目多一点,加油!!!