Leetcode树专题

114 阅读1分钟

总结一下~~~ 刷了两三遍了

1.验证二叉树搜索树 98题

class Solution {
    public boolean isValidBST(TreeNode root) {
        if(root == null){
            return true;
        }
        return dfs(root,Integer.MIN_VALUE,Integer.MAX_VALUE);
    }
    public boolean dfs(TreeNode root, long left ,long right){
        if(root == null){
            return true;
        }
        if(root.val < left || root.val > right){
            return false;
        }
        return dfs(root.left,left,root.val - 1L) &&  dfs(root.right,root.val + 1L,right);
    }
}

中序遍历迭代的解法:

    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        int pre = Integer.MIN_VALUE;
        while(root != null || !stack.isEmpty()){
            if(root != null){
                stack.push(root);
                root = root.left;
            }else{
                TreeNode node = stack.pop();
                if(node < pre){
                    return false;
                }
                pre = node;
                res.add(node.val);
                root = node.right;
            }
        }
        return true;

3.二叉树的前序遍历(非递归版本)

简单

    public List<Integer> preorderTraversal(TreeNode root) {
        if(root == null){
            return new ArrayList<>();
        }
        List<Integer> res = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        stack.add(root);
        while(!stack.isEmpty()){
            TreeNode cur = stack.pop();
            res.add(cur.val);

            if(cur.right != null){
                stack.add(cur.right);
            }
            if(cur.left != null){
                stack.add(cur.left);
            }
        }
        return res;

    }

4.二叉树的后序遍历(非递归版本,可以再做一次)

    public List<Integer> postorderTraversal(TreeNode root) {
        if(root == null){
            return new ArrayList<>();
        }
        List<Integer> res = new ArrayList<>();
        Stack<TreeNode> stack1 = new Stack<>();
        Stack<Integer> stack2 = new Stack<>();
        stack1.push(root);
        while(!stack1.isEmpty()){
            root = stack1.pop();
            if(root.left != null){
                stack1.push(root.left);
            }
            if(root.right != null){
                stack1.push(root.right);
            }
            stack2.push(root.val);
        }
        while(!stack2.isEmpty()){
            res.add(stack2.pop());
        }
        return res;
    }

两个栈实现 

5.101题 判断是否是对称二叉树

递归解法,判断左右节点是否相等

6.105题 重建二叉树

利用一个hasamap来存储位置 优化 比较常规

7.102题 按层打印

用一个queue!解决

8.236题 二叉树的最近公共祖先

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null || root == p || root == q){
            return root;
        }
        TreeNode left = lowestCommonAncestor( root.left,  p,  q);
        TreeNode right = lowestCommonAncestor( root.right,  p,  q);
        if(left == null){
            return right;
        }
        if(right == null){
            return left;
        }
        return root;
    }

 只有几种情况

如果左边 右边都有 那么就是root

如果左边没有,那么就是右边

如果右边没有 那就是左边

如果都没有 返回null

9.543题 树的直径

比较简单,返回左右边最大+1

res 返回左边最大+ 右边最大

10.124题 二叉树中的最大路径和

    int res = Integer.MIN_VALUE;
    public int maxPathSum(TreeNode root) {
        if(root == null){
                return 0;

        }
        dfs(root);
        return res;
    }

    public int dfs(TreeNode root){
        if(root == null){
            return 0;
        }
        int left = dfs(root.left);
        int right = dfs(root.right);
        res = Math.max(res,root.val + left + right);
        return Math.max(0,root.val + Math.max(left,right));
    }

11.序列化二叉树

反序列化的时候用一个队列来实现

public class Codec {

    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        StringBuilder res = new StringBuilder();
        helper4(root,res);
        return res.toString();
    }
    private void helper4(TreeNode root, StringBuilder res){
        if (root == null){
            res.append("null").append(",");
            return;
        }
        res.append(root.val).append(",");
        helper4(root.left,res);
        helper4(root.right,res);
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        Queue<String> queue = new LinkedList<>();
        queue.addAll(Arrays.asList(data.split(",")));
        return buildTree4(queue);
    }
    private TreeNode buildTree4(Queue<String> queue){
        String cur = queue.poll();
        if (cur.equals("null") ){
            return null;
        }
        TreeNode node = new TreeNode(Integer.parseInt(cur));
        node.left = buildTree4(queue);
        node.right = buildTree4(queue);
        return node;
    }

}