Leetcode刷题笔记(5)
主题:二叉树
(1)98. 验证二叉搜索树
思路:中序遍历,前一个节点的值小于后一个节点的值对所有节点均成立则有效;
注意:遍历时,只输出当前节点;
class Solution {
boolean flag = true;
long min = Long.MIN_VALUE;
public boolean isValidBST(TreeNode root) {
if(root == null){
return true;
}
if(root.left != null){
if(min >= root.left.val){
return false;
}else{
flag = isValidBST(root.left);
}
}
if(min >= root.val){
return false;
}else{
min = root.val;
}
if(root.right != null){
if(min >= root.right.val){
return false;
}else{
flag = isValidBST(root.right);
}
}
return flag;
}
}
(2)99. 恢复二叉搜索树
我的思路:bst的中序遍历是有序的,先输出中序遍历树的结果,找到无序的点,如果有两个,则第一个和第二个点的后一个点为错位点,如果只有一个无序点,该点和后一个点为错位点;找到错位的两个点后,找到这两个点的位置,交换节点的值。
class Solution {
public void recoverTree(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
List<Integer> count = new ArrayList<Integer>();
list = inOrderTravel(root, list);
for(int i = 0; i < list.size()-1; i++){
if(list.get(i) > list.get(i+1)){
count.add(i);
}
}
int v1;
int v2;
if(count.size() == 1){
v1 = list.get(count.get(0));
v2 = list.get(count.get(0)+1);
}else{
v1 = list.get(count.get(0));
v2 = list.get(count.get(1)+1);
}
TreeNode node1 = dfs(root,v1);
TreeNode node2 = dfs(root,v2);
if(node1 != null && node2 != null){
node1.val = v2;
node2.val = v1;
}
}
public List<Integer> inOrderTravel(TreeNode root, List<Integer> list){
if(root == null){
return list;
}
if(root.left != null){
inOrderTravel(root.left,list);
}
list.add(root.val);
if(root.right != null){
inOrderTravel(root.right,list);
}
return list;
}
public TreeNode dfs(TreeNode root, int val){
TreeNode node = null;
if(root == null){
return null;
}
if(root.val == val){
node = root;
}
if(node == null && root.left != null){
node = dfs(root.left,val);
}
if(node == null && root.right != null){
node = dfs(root.right,val);
}
return node;
}
}
(3)100. 相同的树
思路:深度优先搜索,递归
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p == null && q == null){
return true;
}else if(p != null && q != null){
if(p.val == q.val){
return isSameTree(p.left,q.left) && isSameTree(p.right, q.right);
}
}
return false;
}
}
(4)450. 删除二叉搜索树中的节点(错1)
按照框架,调用自身,注意利用BST的性质,耐心,细心;
思路:删除节点分为三种情况,无子树,有一个子树,有两个子树,分别处理;注意函数返回值,利用好返回值;
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
if(root == null){
return null;
}
if(key < root.val){
root.left = deleteNode(root.left,key);
return root;
}else if(key > root.val){
root.right = deleteNode(root.right,key);
return root;
}else{
if(root.left == null){
return root.right;
}else if(root.right == null){
return root.left;
}else{
TreeNode minNode = findMin(root.right);
root.val = minNode.val;
minNode.val = key;
root.right = deleteNode(root.right,key);
return root;
}
}
}
private TreeNode findMin(TreeNode root){
if(root.left == null){
return root;
}
return findMin(root.left);
}
}
(5)701. 二叉搜索树中的插入操作
思路:递归
class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
if(root == null){
return new TreeNode(val);
}
if(val < root.val){
root.left = insertIntoBST(root.left, val);
}
if(val > root.val){
root.right = insertIntoBST(root.right, val);
}
return root;
}
}
(6)101. 对称二叉树
思路:递归;由左子树的左子树和右子树的右子树组成的新树,以及由左子树的右子树和右子树的左子树组成的新树,这两棵树也都要是对称的;
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root == null){
return true;
}
if(root.left == null && root.right == null){
return true;
}
if(root.left != null && root.right == null){
return false;
}
if(root.left == null && root.right != null){
return false;
}
if(root.left.val == root.right.val){
return isSymmetric(new TreeNode(0,root.left.left, root.right.right)) && isSymmetric(new TreeNode(0,root.left.right, root.right.left));
}
return false;
}
}
题解思路:迭代,引入队列,初始化将根节点入队两次,每次提取两个节点比较它们的值,然后将两个节点的左右子节点按相反的顺序插入到队列中
class Solution {
public boolean isSymmetric(TreeNode root) {
return check(root, root);
}
public boolean check(TreeNode u, TreeNode v) {
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.offer(u);
q.offer(v);
while (!q.isEmpty()) {
u = q.poll();
v = q.poll();
if (u == null && v == null) {
continue;
}
if ((u == null || v == null) || (u.val != v.val)) {
return false;
}
q.offer(u.left);
q.offer(v.right);
q.offer(u.right);
q.offer(v.left);
}
return true;
}
}
(7)107. 二叉树的层序遍历 II
思路:用一个方法,由上一层的节点得到下一层的所有节点,当层的节点个数不为0时,将这一层的节点的值输出为list加入到res中。
class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
LinkedList<List<Integer>> res = new LinkedList<List<Integer>>();
if(root == null){
return res;
}
List<TreeNode> cache = new ArrayList<TreeNode>();
cache.add(root);
while(cache.size() > 0){
List<Integer> list = new ArrayList<Integer>();
for(TreeNode node : cache){
list.add(node.val);
}
res.addFirst(list);
cache = level(cache);
}
return res;
}
public List<TreeNode> level(List<TreeNode> cache){
List<TreeNode> child = new ArrayList<TreeNode>();
for(TreeNode node : cache){
if(node.left != null){
child.add(node.left);
}
if(node.right != null){
child.add(node.right);
}
}
return child;
}
}
(8)103. 二叉树的锯齿形层序遍历
思路:与上题层序遍历类似,在将该层节点获取节点值转为list时,多加1层层数的判断,奇数层添加到list后面,偶数层添加到list的最前面。
class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(root == null){
return res;
}
List<TreeNode> list = new ArrayList<TreeNode>();
list.add(root);
int level = 0;
while(list.size() > 0){
LinkedList<Integer> temp = new LinkedList<Integer>();
if(level % 2 == 0){
for(TreeNode node: list){
temp.add(node.val);
}
}else{
for(TreeNode node : list){
temp.addFirst(node.val);
}
}
list = getChildren(list);
res.add(temp);
level++;
}
return res;
}
public List<TreeNode> getChildren(List<TreeNode> list){
List<TreeNode> children = new ArrayList<TreeNode>();
for(TreeNode node : list){
if(node.left != null){
children.add(node.left);
}
if(node.right != null){
children.add(node.right);
}
}
return children;
}
}
(9)104. 二叉树的最大深度
我的思路1:递归;但超时了…原因获取最大值的时候进行了四次递归,如果先获得递归的值,再进行比较就会更快。
class Solution {
public int maxDepth(TreeNode root) {
if(root == null){
return 0;
}
int res = maxDepth(root.left) > maxDepth(root.right)? maxDepth(root.left) : maxDepth(root.right);
return res + 1;
}
}
思路2:用list保存下一层的节点,直到list的大小为0;
class Solution {
public int maxDepth(TreeNode root) {
if(root == null){
return 0;
}
List<TreeNode> list = new ArrayList<TreeNode>();
list.add(root);
int level = 0;
while(list.size() > 0){
list = getChildren(list);
level++;
}
return level;
}
public List<TreeNode> getChildren(List<TreeNode> list){
List<TreeNode> children = new ArrayList<TreeNode>();
for(TreeNode node : list){
if(node.left != null){
children.add(node.left);
}
if(node.right != null){
children.add(node.right);
}
}
return children;
}
}
(10)105. 从前序与中序遍历序列构造二叉树(错1)
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
return build(preorder, 0 ,preorder.length-1, inorder, 0, inorder.length-1);
}
public TreeNode build(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd){
if(preStart > preEnd){
return null;
}
int rootVal = preorder[preStart];
int index = 0;
for(int i = inStart; i <= inEnd; i++){
if(inorder[i] == rootVal){
index = i;
break;
}
}
int leftSize = index - inStart;
TreeNode root = new TreeNode(rootVal);
root.left = build(preorder,preStart+1,preStart+leftSize, inorder, inStart,index-1);
root.right = build(preorder, preStart+leftSize+1, preEnd, inorder, index+1, inEnd);
return root;
}
}