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

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

indexindex_剑指offerdescriptionkey wordsdonedate对应leetcodedone
17重建二叉树遍历,重建Y19-12-2105,106N
28二叉树的下一个结点下一结点Y19-12-3Y
326树的子结构遍历,递归N19-12-3572Y
427二叉树的镜像遍历,镜像 ,递归N19-12-18226Y
528对称的二叉树遍历,对称N19-12-3101N
632从上往下打印二叉树层序遍历N19-12-3102,107N
733二叉搜索树的后序遍历序列BSTN19-12-3225N
834二叉树中和为某一值的路径路径和N19-12-3113, 112,437N
936二叉搜索树与双向链表BST与链表N19-12-3109N
1037序列化二叉树序列化N19-12-3
1154二叉搜索树的第k大结点BST,中序遍历N19-12-3230N
1255二叉树的深度树的深度N19-12-18104,111Y
1355_2判断是否是AVL树深度,AVL树N19-12-3110N
1468树中两个结点的最低公共祖先公共祖先,LCAN19-12-3235,236N
15把二叉树打印成多行层序遍历N19-12-3
16按之字顺序打印二叉树层序遍历N19-12-3103N
17前序遍历遍历Y19-12-13144Y
18中序遍历遍历Y19-12-1394Y
19后序遍历遍历Y19-12-13145Y
20层序遍历遍历N19-12-18102Y
21Binary Tree Right Side View层序遍历N19-12-3199N
22根据前序和后序遍历构造二叉树遍历N19-12-3889N
23平衡二叉树递归N19-12-18110Y

大佬写的很好的专门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;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值