算法练习LeetCode初级算法之树
-
二叉树的前序遍历
-
我的解法:利用递归,自底向下逐步添加到list,返回最终的前序遍历list
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list=new ArrayList<>();
if (root==null) {
return list;
}
list.add(root.val);
if (root.left!=null) {
list.addAll(preorderTraversal(root.left));
}
if (root.right!=null) {
list.addAll(preorderTraversal(root.right));
}
return list;
}
}
-
参考解法:利用递归,但只在外部建一个list,更好理解!
class Solution {
public List<Integer> list=new LinkedList<>();
public List<Integer> preorderTraversal(TreeNode root) {
if (root==null)
return list;
list.add(root.val);
preorderTraversal(root.left);
preorderTraversal(root.right);
return list;
}
}
-
中序遍历二叉树,同样有两种方法
-
第一种
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list=new ArrayList<>();
if (root==null) {
return list;
}
if (root.left!=null) {
list.addAll(inorderTraversal(root.left));
}
list.add(root.val);
if (root.right!=null) {
list.addAll(inorderTraversal(root.right));
}
return list;
}
}
-
第二种
class Solution {
List<Integer> list=new ArrayList<>();
public List<Integer> inorderTraversal(TreeNode root) {
if (root==null) {
return list;
}
inorderTraversal(root.left);
list.add(root.val);
inorderTraversal(root.right);
return list;
}
}
-
后序遍历二叉树:也有两种方法,和前面的差不多,所以只写简洁的
class Solution {
List<Integer> list=new ArrayList<>();
public List<Integer> postorderTraversal(TreeNode root) {
if (root==null) {
return list;
}
postorderTraversal(root.left);
postorderTraversal(root.right);
list.add(root.val);
return list;
}
}
-
层次遍历二叉树
-
队列解法:
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res=new ArrayList<>();
if (root==null) {
return res;
}
Queue<TreeNode> queue=new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
int count=queue.size();
List<Integer> list=new LinkedList<>();
while (count>0) {
TreeNode node=queue.poll();
list.add(node.val);
if (node.left!=null) {
queue.add(node.left);
}
if (node.right!=null) {
queue.add(node.right);
}
count--;
}
res.add(list);
}
return res;
}
}
-
递归解法:参考大神的代码!!!
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res=new ArrayList<>();
if (root==null) {
return res;
}
addList(res, 0, root);
return res;
}
private void addList(List<List<Integer>> res,int level,TreeNode head) {
if (head==null) {
return;
}
if (res.size()<=level) { //这里有个问题,如果不是等于的话
res.add(new ArrayList<>());
}
res.get(level).add(head.val);//这里的将会越界,因为level=res.size()取不到
addList(res, level+1, head.left);
addList(res, level+1, head.right);
}
}
-
二叉树的最大深度
-
递归
class Solution {
public int maxDepth(TreeNode root) {
if (root==null) {
return 0;
}
int leftH=maxDepth(root.left);
int rightH=maxDepth(root.right);
return Math.max(leftH, rightH)+1;
}
}
-
迭代
这个方法太难了,不优先考虑!!
class Solution {
public int maxDepth(TreeNode root) {
Queue<Pair<TreeNode,Integer>> queue=new LinkedList<>();
if (root!=null) {
queue.add(new Pair<TreeNode, Integer>(root, 1));
}
int depth=0;
while (!queue.isEmpty()) {
Pair<TreeNode,Integer> pair=queue.poll();
root=pair.getKey();
int pair_depth=pair.getValue();
if (root!=null) {
depth=Math.max(depth, pair_depth);
queue.add(new Pair<TreeNode, Integer>(root.left, pair_depth+1));
queue.add(new Pair<TreeNode, Integer>(root.right, pair_depth+1));
}
}
return depth;
}
}
-
对称二叉树
-
递归
class Solution {
public boolean isSymmetric(TreeNode root) {
return isMirror(root, root);
}
private boolean isMirror(TreeNode t1,TreeNode t2) {
if (t1==null&&t2==null) {
return true;
}
if (t1==null||t2==null) {
return false;
}
return (t1.val==t2.val)&&isMirror(t1.left, t2.right)
&&isMirror(t1.right,t2.left);
}
}
-
迭代
class Solution {
public boolean isSymmetric(TreeNode root) {
Queue<TreeNode> queue=new LinkedList<>();
if (root==null||(root.left==null&&root.right==null)) {
return true;
}
queue.add(root.left);
queue.add(root.right);
while (!queue.isEmpty()) {
TreeNode t1=queue.poll();
TreeNode t2=queue.poll();
if (t1==null&&t2==null) continue;
if(t1==null||t2==null) return false;
if(t1.val!=t2.val) return false;
queue.add(t1.left);
queue.add(t2.right);
queue.add(t1.right);
queue.add(t2.left);
}
return true;
}
}
-
路径总和:递归很简洁
class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if (root==null) {
return false;
}
if (root.left==null&&root.right==null) {
return sum-root.val==0;
}
return hasPathSum(root.right, sum-root.val)||
hasPathSum(root.left, sum-root.val);
}
}
-
验证二叉搜索树
-
利用中序遍历法:简单易懂
class Solution {
public boolean isValidBST(TreeNode root) {
if (root==null) {
return true;
}
List<Integer> list=new ArrayList<>();
inOrder(root, list);
for (int i = 0; i < list.size()-1; i++) {
if (list.get(i+1)<=list.get(i)) {
return false;
}
}
return true;
}
private void inOrder(TreeNode node,List<Integer> list) {
if (node==null) {
return;
}
inOrder(node.left, list);
list.add(node.val);
inOrder(node.right, list);
}
}
-
大神递归法:
class Solution {
double last=-Double.MAX_VALUE;
public boolean isValidBST(TreeNode root) {
if (root==null) {
return true;
}
if (isValidBST(root.left)) {
if (last<root.val) {
last=root.val;
return isValidBST(root.right);
}
}
return false;
}
}
-
堆桟法
public boolean isValidBST(TreeNode root) {
Stack<TreeNode> stack = new Stack();
TreeNode p = root;
Integer preVal = null ;
while( p != null || !stack.isEmpty() ){
if(p != null){
stack.push(p);
p = p.left;
}else{
p = stack.pop();
int val = p.val;
if(preVal == null){
preVal = val;
}else{
if(val <= preVal){
return false;
}
preVal = val;
}
p = p.right;
}
}
return true;
}
-
将有序数组转换为二叉搜索树
-
解法一
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
return buildBST(nums, 0, nums.length-1);
}
private TreeNode buildBST(int[] nums,int l,int r) {
if (l>r) {
return null;
}
if (l==r) {
return new TreeNode(nums[l]);
}
int mid=(r+l)/2;
TreeNode root=new TreeNode(nums[mid]);
root.left=buildBST(nums, l, mid-1);
root.right=buildBST(nums, mid+1, r);
return root;
}
}
-
总结:递归是万能的,但递归真的很恶心!!!