刷题--剑指offer+leetcode--专题--树

未完待续。。。
可视化:
https://visualgo.net/zh
剑指offer Java版https://www.jianshu.com/p/164716256c06
主要是剑指offer的:
剑指offer第二版+leetcode:

index index_剑指offer description key words done date 对应leetcode done
1 7 重建二叉树 遍历,重建 Y 19-12-2 105,106 N
2 8 二叉树的下一个结点 下一结点 Y 19-12-3 Y
3 26 树的子结构 遍历,递归 N 19-12-3 572 Y
4 27 二叉树的镜像 遍历,镜像 ,递归 N 19-12-18 226 Y
5 28 对称的二叉树 遍历,对称 N 19-12-3 101 N
6 32 从上往下打印二叉树 层序遍历 N 19-12-3 102,107 N
7 33 二叉搜索树的后序遍历序列 BST N 19-12-3 225 N
8 34 二叉树中和为某一值的路径 路径和 N 19-12-3 113, 112,437 N
9 36 二叉搜索树与双向链表 BST与链表 N 19-12-3 109 N
10 37 序列化二叉树 序列化 N 19-12-3
11 54 二叉搜索树的第k大结点 BST,中序遍历 N 19-12-3 230 N
12 55 二叉树的深度 树的深度 N 19-12-18 104,111 Y
13 55_2 判断是否是AVL树 深度,AVL树 N 19-12-3 110 N
14 68 树中两个结点的最低公共祖先 公共祖先,LCA N 19-12-3 235,236 N
15 把二叉树打印成多行 层序遍历 N 19-12-3
16 按之字顺序打印二叉树 层序遍历 N 19-12-3 103 N
17 前序遍历 遍历 Y 19-12-13 144 Y
18 中序遍历 遍历 Y 19-12-13 94 Y
19 后序遍历 遍历 Y 19-12-13 145 Y
20 层序遍历 遍历 N 19-12-18 102 Y
21 Binary Tree Right Side View 层序遍历 N 19-12-3 199 N
22 根据前序和后序遍历构造二叉树 遍历 N 19-12-3 889 N
23 平衡二叉树 递归 N 19-12-18 110 Y

大佬写的很好的专门leetcode的二叉树专题:
https://www.jianshu.com/p/7c8a4f71675d
LeetCode 二叉树系统题解:
https://www.jianshu.com/p/e0bbf80f7541
各种leetcode总结:
https://www.jianshu.com/p/97bb455da917

  1. 重建二叉树
/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
import java.util.Arrays;
public class Solution {
    public TreeNode reConstructBinaryTree(int[] pre,int[] in) {
        TreeNode root = reConstructBinaryTree(pre,0,pre.length-1,in,0, in.length-1);
        return root;
    }
    public TreeNode reConstructBinaryTree(int[] pre, int preStart, int preEnd, int[] in, int inStart, int inEnd){
        if (preStart > preEnd || inStart > inEnd) {
            return null;
        }
        int rootVal = pre[preStart];
        TreeNode root = new TreeNode(rootVal);
        for(int i= inStart; i <= inEnd; i++){
            if(in[i]==rootVal){
                root.left = reConstructBinaryTree(pre,preStart+1,preStart+i-inStart, in, inStart,i-1);
                root.right = reConstructBinaryTree(pre,preStart+i-inStart+1, preEnd, in, i+1,inEnd);
            }
        }
        return root;
    }
}
  1. 二叉树的下一个节点
TreeLinkNode GetNext(TreeLinkNode node)
    {
        if(node==null) return null;
        if(node.right!=null){    //如果有右子树,则找右子树的最左节点
            node = node.right;
            while(node.left!=null) node = node.left;
            return node;
        }
        while(node.next!=null){ //没右子树,则找第一个当前节点是父节点左孩子的节点
            if(node.next.left==node) return node.next;
            node = node.next;
        }
        return null;   //退到了根节点仍没找到,则返回null
    }
  1. 树的子结构
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isIdentical(TreeNode s, TreeNode t){
        if(s == null && t == null) return true;
        if(s == null || t == null) return false;
        if(s.val != t.val) return false;
        return isIdentical(s.left,t.left) && isIdentical(s.right,t.right); 
    }
    public boolean isSubtree(TreeNode s, TreeNode t) {
        if(s == null) return false;
        if(isIdentical(s,t)) return true; 
        return isSubtree(s.left,t) || isSubtree(s.right,t);
    }
}
  1. 二叉树的镜像
//我的思路是层序遍历之后把每层的list反转,感觉有点复杂。。然后发现不行,人家返回的要求是node。。这样的话返回是lists,而且除了遍历之外还要多一步反转链表
//剑指offer书里的思路是前序遍历之后如果节点有左右节点就交换左右子节点
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode invertTree(TreeNode root) {
       if(root == null) return root;
       if(root.left == null && root.right == null){
         return root;
       }
       TreeNode tempnode = root.left;
       root.left = root.right;
       root.right = tempnode;
       if(root.left != null){
           invertTree(root.left);
       }
       if(root.right != null){
           invertTree(root.right);
       }
       return root;
    }
}

  1. 二叉树深度
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null) return 0;
        int left = maxDepth(root.left) + 1;
        int right = maxDepth(root.right) + 1;
        return Math.max(left,right);
    }
}
  1. 前序遍历
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    
    public void robot(TreeNode p, List<Integer> ans) {
        if(p == null) return;
        // 中左右
        ans.add(p.val);
        robot(p.left, ans);
        robot(p.right, ans);
    }
    
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<Integer>();
        robot(root, ans);
        return ans;
    }
}

非递归:

/**
 * 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<Integer> preorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<Integer>();
        Stack<TreeNode> stk = new Stack<>();
        //TreeNode node = root;
        
        if (root == null) {
            return ans;
        }
        stk.push(root);
        while(!stk.isEmpty()){
            TreeNode node = stk.pop();
            ans.add(node.val);
            
            if(node.right != null){
               stk.push(node.right);
            }
            if(node.left != null){
               stk.push(node.left);
            }
        }
        return ans;
    }
}
  1. 中序遍历
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public void inorder(TreeNode root, List<Integer> list){
        if(root == null){
            return ;
        }
        //左根右
        inorder(root.left, list);
        list.add(root.val);
        inorder(root.right,list);
    }
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<Integer>();
        inorder(root,list);
        return list;
    }
}

stack遍历:

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode first = root;
        while(first != null || stack.isEmpty() != true){
            while(first != null){
                stack.push(first);
                first = first.left;
            }
            first = stack.pop();
            list.add(first.val);
            first = first.right;
        }
        return list;
    }
}
  1. 后序遍历
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<Integer>();
        Stack<TreeNode> stk = new Stack<>();
        //TreeNode node = root;
        
        if (root == null) {
            return ans;
        }
        stk.push(root);
        while(!stk.isEmpty()){
            TreeNode node = stk.pop();
            if(node.left != null){
               stk.push(node.left);
            }
            if(node.right != null){
               stk.push(node.right);
            }
            ans.add(0,node.val);
            
        }

        return ans;
    }
}
  1. 层序遍历
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

 
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> valueList = new ArrayList<>();
        if(root == null) return valueList;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            int level = queue.size();
            List<Integer> list = new ArrayList<>();
            for(int i = 0; i < level; i++){
                TreeNode node = queue.poll();
                list.add(node.val);
                if(node.left != null){
                    queue.add(node.left);
                }
                if(node.right != null){
                    queue.add(node.right);
                }
            }
            //level++;
           valueList.add(list);
        }
        return valueList;
    }
}
  1. 平衡二叉树
//平衡二叉树:一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    boolean res = true;
    public int height(TreeNode root){
        if(root == null) return 0;
        int left = height(root.left) + 1;
        int right = height(root.right) + 1;
        if(Math.abs(left-right) > 1) res = false;
        return Math.max(left,right);
    }
    public boolean isBalanced(TreeNode root) {
        height(root);
        return res;
    }
}

你可能感兴趣的:(刷题,日常)