力扣labuladong——一刷day05

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、力扣104. 二叉树的最大深度
  • 二、力扣543. 二叉树的直径
  • 三、力扣144. 二叉树的前序遍历


前言


一、力扣104. 二叉树的最大深度

遍历

/**
 * 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 depth = 0,res = 0;
    public int maxDepth(TreeNode root) {
        traverser(root);
        return depth;
    }
    public void traverser(TreeNode root){
        if(root == null){
            return ;
        }
        res ++;
        depth = Math.max(depth,res);
        traverser(root.left);
        traverser(root.right);
        res --;
    }
}

分解

/**
 * 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 depth(root);
    }
    public int depth(TreeNode root){
        if(root == null){
            return 0;
        }
        int l = depth(root.left);
        int r = depth(root.right);
        return l > r ? l + 1 : r + 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 path = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        fun(root);
        return path-1;
    }
    public int fun(TreeNode root){
        if(root == null){
            return 0;
        }
        int l = fun(root.left);
        int r = fun(root.right);
        path = Math.max(path, l + r + 1);
        return l > r ? l + 1:r + 1;
    }
}

三、力扣144. 二叉树的前序遍历

分解的思路

/**
 * 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 List<Integer> preorderTraversal(TreeNode root) {
        return fun(root);
    }
    public List<Integer> fun(TreeNode root){
        List<Integer> arr = new LinkedList<>();
        if(root == null){
            return arr;
        }
        arr.add(root.val);
        arr.addAll(fun(root.left));
        arr.addAll(fun(root.right));
        return arr;
    }
}

你可能感兴趣的:(leetcode,算法,java,数据结构)