Leetcode二叉树专题

104. 二叉树的最大深度

在这里插入图片描述

  1. 用dfs 遍历一遍二叉树得出答案
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */

 //解法1:通过遍历DFS类似
class Solution {
    int count=0;
    int max=0;
    public int maxDepth(TreeNode root) {
        dfs(root);
        return max;

    }
    public void dfs(TreeNode root){
        if(root==null){
            max=Math.max(count,max);
            return;
        }
        count++;
        dfs(root.left);
        count--;

        count++;
        dfs(root.right);
        count--;
    }
}
  1. 用动态规划的思想,递归得出答案
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int maxDepth(TreeNode root) {
        return dp(root);
    }
    public int dp(TreeNode root){
        if(root==null) return 0;
        return Math.max(dp(root.left)+1,dp(root.right)+1);

    }
}
543. 二叉树的直径
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */

class Solution {
    int maxCount=0;
    public int diameterOfBinaryTree(TreeNode root) {
        maxDept(root);
        return maxCount;
    }

    public int maxDept(TreeNode root){
        if(root==null) return 0;
        int leftCount= maxDept(root.left);
        int rightCount= maxDept(root.right);

        int myCount=leftCount+rightCount;
        maxCount=Math.max(myCount,maxCount);
        return 1+Math.max(leftCount,rightCount);
    }
}
226. 翻转二叉树

①遍历:

// 主函数
TreeNode invertTree(TreeNode root) {
    // 遍历二叉树,交换每个节点的子节点
    traverse(root);
    return root;
}

// 二叉树遍历函数
void traverse(TreeNode root) {
    if (root == null) {
        return;
    }

    /**** 前序位置 ****/
    // 每一个节点需要做的事就是交换它的左右子节点
    TreeNode tmp = root.left;
    root.left = root.right;
    root.right = tmp;

    // 遍历框架,去遍历左右子树的节点
    traverse(root.left);
    traverse(root.right);
}

② 分解问题思路:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    //假想invertTree 函数是将root 节点的左右子树交换了的结果;
    public TreeNode invertTree(TreeNode root) {
        if(root==null) return null;
        TreeNode left= invertTree(root.left);
        TreeNode right=invertTree(root.right);
        root.left=right;
        root.right=left;
        return root;
    }
}
116. 填充每个节点的下一个右侧节点指针

①遍历每个节点

/*
// Definition for a Node.
class Node {
    public int val;
    public Node left;
    public Node right;
    public Node next;

    public Node() {}
    
    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, Node _left, Node _right, Node _next) {
        val = _val;
        left = _left;
        right = _right;
        next = _next;
    }
};
*/

class Solution {
    public Node connect(Node root) {
        if(root==null) return root;
        traverse(root.left,root.right);
        return root;
    }
    public void traverse(Node root1,Node root2){
       if(root1==null||root2==null) return;
       root1.next=root2;
       traverse(root1.left,root1.right);
       traverse(root1.right,root2.left);
       traverse(root2.left,root2.right);
    }
}
114. 二叉树展开为链表

用分解法,分三步

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public void flatten(TreeNode root) {
       dp(root);
    }
    //将以root 为根节点的树,展开为一条链表
    public void dp(TreeNode root){
        if(root==null) return;
        //将左右子树都变成一条链
       dp(root.left);
       dp(root.right);
       //将root 的右指针指向左节点
       TreeNode left= root.left;
       TreeNode right= root.right;
       root.right=left;
       root.left=null;
       //将root 的原来右子树拼接到变成链的左子树的下面
       while(root!=null&&root.right!=null){
           root=root.right;
       }
       root.right=right;     
    }
}
654. 最大二叉树

如果单独抽出一个二叉树节点,它需要做什么事情?需要在什么时候(前/中/后序位置)做?其他的节点不用你操心,递归函数会帮你在所有节点上执行相同的操作。

二叉树的构造问题一般都是使用「分解问题」的思路:构造整棵树 = 根节点 + 构造左子树 + 构造右子树。用分解法

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        int n=nums.length;
        return dp(nums,0,n-1);
    }
    //表示构建了一个以root为根的最大二叉树
    public TreeNode dp(int[] nums,int start,int end){
        if(start>end) return null;

         //找出数组中最大值和最大的索引
         int index=-1; int max=Integer.MIN_VALUE;
         for(int i=start;i<=end;i++){
             if(nums[i]>max){
                 max=nums[i];
                 index=i;
             }
         }
        TreeNode root=new TreeNode(max);

        TreeNode l= dp(nums,start,index-1);
        root.left=l;

        TreeNode r= dp(nums,index+1,end);
        root.right=r;

        return root;
    }

}
105. 从前序与中序遍历序列构造二叉树
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        int n=preorder.length;
        return build(preorder,0,n-1,inorder,0,n-1);
    }
    //构建一个以root 为根的二叉树
    public TreeNode build(int[] preorder,int preStart,int preEnd,int[] inorder,int inStart,int inEnd){
        if(preStart>preEnd) return null;
        int preVal=preorder[preStart];
        int index=-1;
        for(int i=inStart;i<=inEnd;i++){
            if(inorder[i]==preVal){
                index=i;
                break;
            }
        }
        int leftSize=index-inStart;
        TreeNode root=new TreeNode(preVal);

        root.left= build(preorder,preStart+1,preStart+leftSize,inorder,inStart,index-1);
        root.right=build(preorder,preStart+leftSize+1,preEnd,inorder,index+1,inEnd);
        return root;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值