前言
没想到大三依然课业繁重,再加上一些琐事缠身,以及自身的惰性,不少计划都是心有余力不足,但又不愿这么混下去,近来回顾了一下自己在学习算法技能树上的加点,发现一些基础算法与经典算法实在是有那么点囫囵吞枣,所以有个刷leetcode的打算,一是为了温故知新,二是为了熟悉Java或Python,三是为了保持编程手感,四是为以后工作做个铺垫?
目标大概是有生之年内用Java/Python/C++/C刷一遍leetcode上有趣的题。【原则上first Java,second Python,third C/C++】
而具体计划呢则是没有的,印象中上了大学后,只要说出来或写出来的计划都会被各种不可抗力因素打断(比如本博客就有很多未完待续的坑。。),所以能刷一题是一题,能学一点是一点,博客尽量更新,这学期结束估计就主要忙考研的事去了,而这两个月也是有各种考试报告要腾出手去处理,所以。。这TM又是一个大坑!
Easy
Problem 100. Same Tree
题意
判断两棵树是否相同.
思路
见代码.
代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null)
return true;
if (p == null && q != null)
return false;
if (p != null && q == null)
return false;
if (p != null && q != null && p.val == q.val)
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
return false;
}
}
Problem 101. Symmetric Tree
题意
判断一颗二叉树是否镜像对称
思路
递归写起来要简洁一些..
代码
// recursive
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean mySymmetric(TreeNode leftNode, TreeNode rightNode) {
if (leftNode != null && rightNode != null && leftNode.val == rightNode.val)
return mySymmetric(leftNode.left, rightNode.right) && mySymmetric(leftNode.right, rightNode.left);
else if (leftNode == null && rightNode == null)
return true;
else return false;
}
public boolean isSymmetric(TreeNode root) {
if (root == null) return true;
else return mySymmetric(root.left, root.right);
}
}
// iterative
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSymmetric(TreeNode root) {
Queue<TreeNode> q = new LinkedList();
q.add(root);
q.add(root);
while (!q.isEmpty()) {
TreeNode leftNode = q.poll();
TreeNode rightNode = q.poll();
if (leftNode == null && rightNode == null) continue;
else if (leftNode == null || rightNode == null) return false;
else if (leftNode.val != rightNode.val) return false;
q.add(leftNode.left);
q.add(rightNode.right);
q.add(leftNode.right);
q.add(rightNode.left);
}
return true;
}
}
Problem 102. Binary Tree Level Order Traversal
题意
输出二叉树每一层的元素
思路
思路没什么难的,就是语法上纠结了一下。。
关于List的各种方法以及实现可以参考这个:
ArrayList方法的实现
List的接口与使用示例
代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> treeList = new ArrayList<List<Integer>>();
public void traversal(TreeNode root, int depth) {
if (root == null) return;
if(treeList.size() == depth) treeList.add(new ArrayList<Integer>());
treeList.get(depth).add(root.val);
traversal(root.left, depth + 1);
traversal(root.right, depth + 1);
}
public List<List<Integer>> levelOrder(TreeNode root) {
traversal(root, 0);
return treeList;
}
}
Problem 107. Binary Tree Level Order Traversal II
题意
从一颗二叉树底部开始输出每一层节点的值
思路
102的小小进阶,没啥难点,如果是cpp或者python的话可以直接用reverse函数就行,不知道为啥java的Collections.reverse()不行。。所以直接改的list插入位置。
update:Collections.reverse()【无返回值】也可以
代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> treeList = new ArrayList<List<Integer>>();
public void traversal(TreeNode root, int depth) {
if (root == null) return;
if(treeList.size() == depth) treeList.add(0, new ArrayList<Integer>());
treeList.get(treeList.size() - depth - 1).add(root.val);
traversal(root.left, depth + 1);
traversal(root.right, depth + 1);
}
public List<List<Integer>> levelOrderBottom(TreeNode root) {
traversal(root, 0);
// Collections.reverse(treeList);
return treeList;
}
}
Problem 110. Balanced Binary Tree
题意
判断一颗二叉树是否平衡(左右子树高度相差不超过1)
思路
求下高度就ok。
代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int getDepth(TreeNode root) {
if (root == null)
return 0;
else return Math.max(getDepth(root.left), getDepth(root.right)) + 1;
}
public boolean isBalanced(TreeNode root) {
if (root == null)
return true;
int leftDepth = getDepth(root.left);
int rightDepth = getDepth(root.right);
int diff = Math.abs(leftDepth - rightDepth);
if (diff > 1) return false;
else return isBalanced(root.left) && isBalanced(root.right);
}
}
Problem 111. Minimum Depth of Binary Tree
题意
求一颗二叉树根节点到最近叶子节点的最短路径要经过多少个节点。
思路
和求高度的思路比较相似。
代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int minDepth(TreeNode root) {
if (root == null)
return 0;
if (root.left == null && root.right == null)
return 1;
else if (root.left != null && root.right == null)
return minDepth(root.left) + 1;
else if (root.left == null && root.right != null)
return minDepth(root.right) + 1;
else return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
}
}
Problem 112. Path Sum
题意
输入一颗二叉树与一个整数sum,求是否有一条根节点到叶节点的路径,使得路径经过的节点值之和恰好等于sum。
思路
普通的dfs。
代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if (root == null) return false;
else if (root.left == null && root.right == null && sum == root.val)
return true;
else return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
}
}
Problem 226. Invert Binary Tree
题意
just invert a binary tree.
ps. 传说中干掉Max Howell的Google面试题233
思路
依然普通的dfs…
代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null)
return root;
TreeNode right = invertTree(root.right);
TreeNode left = invertTree(root.left);
root.left = right;
root.right = left;
return root;
}
}
Problem 235. Lowest Common Ancestor of a Binary Search Tree
题意
输入一棵BST,以及两个节点p,q,求p与q的LCA。
思路
由于BST的特性(左小右大),p与q的LCA节点的值一定处于p与q的值之间,即
pval≤