Leetcode - Tree - Easy(111-404)

  1. Minimum Depth of Binary Tree
    The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
/**
 * DFS思路,代码比较简洁,但如果是左侧500个单链孩子,右侧1个孩子这种比较极端的情况,时间复杂度会太高
 * /
class Solution1 {
    public int minDepth(TreeNode root) {
        if(root==null){return 0;}
        if(root.left==null && root.right==null){return 1;}
        if(root.left==null){return minDepth(root.right)+1;}
        if(root.right==null){return minDepth(root.left)+1;}
        return Math.min(minDepth(root.left),minDepth(root.right))+1;
    }
}
/**
 * BFS思路,faster than 100.00% 但是空间复杂度略高。
 * 结论:一般来说在找最短路径的时候使用 BFS,其他时候还是 DFS 使用得多一些(主要是递归代码好写)
 * /
class Solution2 {
    public int minDepth(TreeNode root) {
        if(root==null){return 0;}
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        int depth=1;
        
        while(!q.isEmpty()){
            int sz = q.size();
            
            for(int i=0;i<sz;i++){
                TreeNode current = q.poll();
                if(current.left==null && current.right==null){return depth;}
                if(current.left!=null){
                     q.add(current.left);
                 }
                if(current.right!=null){
                     q.add(current.right);
                 }   
            }      
            
            depth++;
        }
        return 0;
    }    
}
  1. Path Sum
    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. Note: A leaf is a node with no children.
    Example:
    Given the below binary tree and sum = 22,
      5
     / \
    4   8
   /   / \
  11  13  4
 /  \      \
7    2      1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

/**
 * DFS思路, 除去root是否满足条件的判断之外,重点在于看左右孩子中有没有满足sum-root.val的路径
 * /
class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
        if(root==null){return false;}
        if(root.val==sum && root.left==null && root.right==null){return true;}
        if(hasPathSum(root.left,sum-root.val)){return true;}
        if(hasPathSum(root.right,sum-root.val)){return true;}
        return false;
    }
}

进阶版:
437. Binary Tree Paths
Find the number of paths that sum to a given value.
The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).
Example:
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8

      10
     /  \
    5   -3
   / \    \
  3   2   11
 / \   \
3  -2   1

Return 3. The paths that sum to 8 are:

  1. 5 -> 3
  2. 5 -> 2 -> 1
  3. -3 -> 11
class Solution {
    public int pathSum(TreeNode root, int sum) {
        if(root==null){return 0;}
        return dfs(root,sum)+pathSum(root.left,sum)+pathSum(root.right,sum);
    }
    private int dfs(TreeNode root,int sum){
        int res=0;
        if(root==null){return res;}
        if(root.val==sum){res++;}
        res+=dfs(root.left,sum-root.val);
        res+=dfs(root.right,sum-root.val);
        return res;
    }
}

leetcode discussion: better solution using hash map
115. Invert Binary Tree
Example:
Input:

     4
   /   \
  2     7
 / \   / \
1   3 6   9

Output:

     4
   /   \
  7     2
 / \   / \
9   6 3   1
/**
 * DFS递归思路,faster than 100.00% of Java online submissions for Invert Binary Tree.
 */
class Solution1 {
    public TreeNode invertTree(TreeNode root) {
        if(root==null){return null;}
        TreeNode tmp = root.left;
        root.left=root.right;
        root.right=tmp;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
}
/**
 * BFS思路,复杂度比solution1更好些
 */
class Solution2 {
    public TreeNode invertTree(TreeNode root) {
        if(root==null){return null;}
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        
        while(!q.isEmpty()){
            int sz = q.size();
            for(int i=0;i<sz;i++){
                TreeNode current = q.poll();
                TreeNode tmp = current.left;
                current.left = current.right;
                current.right = tmp;
                if(current.left!=null){q.offer(current.left);}
                if(current.right!=null){q.offer(current.right);}
            }
        }
        return root;
    }
}
  1. Lowest Common Ancestor of a Binary Search Tree
    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
    According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

Example 1:
在这里插入图片描述

Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
Output: 6
Explanation: The LCA of nodes 2 and 8 is 6.

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root==null){return null;}
        if(root==p || root==q){return root;}
        if(root.val>Math.max(p.val,q.val)){
           root = lowestCommonAncestor(root.left,p,q);
        }else if(root.val< Math.min(p.val,q.val)){
           root = lowestCommonAncestor(root.right,p,q);
        }
        return root;
    }
}
  1. Binary Tree Paths 【重点看】
    Example:

Input:

   1
 /   \
2     3
 \
  5

Output: [“1->2->5”, “1->3”]

Explanation: All root-to-leaf paths are: 1->2->5, 1->3

/**
 * 好理解的DFS版本,String is immutable所以每次都会建立新的
 * Runtime: 10 ms, Memory Usage: 40.5 MB
 */
class Solution1 {
    public List<String> binaryTreePaths(TreeNode root) {
        ArrayList<String> result = new ArrayList<>();
        binaryTreePathsHelper(root,"",result);
        return result;
    }
    private void binaryTreePathsHelper(TreeNode root, String solution, ArrayList<String> result){
        if(root==null){return;}
        if(root.left==null && root.right==null){result.add(solution+root.val);}
        binaryTreePathsHelper(root.left,solution+root.val+"->",result);
        binaryTreePathsHelper(root.right,solution+root.val+"->",result);
    }
}
/**
 *  改进版: We are passing object(address) of StringBuilder in recursion.
 *  StringBuilder is mutable- only one object(stringbuilder) would be created, so to avoid 
 *  retaining the previous list of values, we set the length to restrict them to go over to the next level.
 *  Runtime: 1 ms, Memory Usage: 39.5 MB
 */
class Solution2 {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<>();
        StringBuilder sb= new StringBuilder();
        helper(res,root,sb);
        return res;
    }

    private void helper(List<String> res, TreeNode root, StringBuilder sb) {
        if(root == null) {
            return;
        }
        int len = sb.length();
        sb.append(root.val);
        if(root.left == null && root.right == null) {
            res.add(sb.toString());
        } else {
            sb.append("->");
            helper(res, root.left, sb);
            helper(res, root.right, sb);
        }
        sb.setLength(len);
    }
}
  1. Sum of Left Leaves
    Find the sum of all left leaves in a given binary tree.
    Example:
    3
   / \
  9  20
    /  \
   15   7

There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.

class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        if(root==null){return 0;}
        int sum = 0;
        if(root.left!=null && root.left.left==null&& root.left.right==null){
            sum+=root.left.val;
        }
        sum+=sumOfLeftLeaves(root.left);
        sum+=sumOfLeftLeaves(root.right);
        return sum;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于微信小程序的家政服务预约系统采用PHP语言和微信小程序技术,数据库采用Mysql,运行软件为微信开发者工具。本系统实现了管理员和客户、员工三个角色的功能。管理员的功能为客户管理、员工管理、家政服务管理、服务预约管理、员工风采管理、客户需求管理、接单管理等。客户的功能为查看家政服务进行预约和发布自己的需求以及管理预约信息和接单信息等。员工可以查看预约信息和进行接单。本系统实现了网上预约家政服务的流程化管理,可以帮助工作人员的管理工作和帮助客户查询家政服务的相关信息,改变了客户找家政服务的方式,提高了预约家政服务的效率。 本系统是针对网上预约家政服务开发的工作管理系统,包括到所有的工作内容。可以使网上预约家政服务的工作合理化和流程化。本系统包括手机端设计和电脑端设计,有界面和数据库。本系统的使用角色分为管理员和客户、员工三个身份。管理员可以管理系统里的所有信息。员工可以发布服务信息和查询客户的需求进行接单。客户可以发布需求和预约家政服务以及管理预约信息、接单信息。 本功能可以实现家政服务信息的查询和删除,管理员添加家政服务信息功能填写正确的信息就可以实现家政服务信息的添加,点击家政服务信息管理功能可以看到基于微信小程序的家政服务预约系统里所有家政服务的信息,在添加家政服务信息的界面里需要填写标题信息,当信息填写不正确就会造成家政服务信息添加失败。员工风采信息可以使客户更好的了解员工。员工风采信息管理的流程为,管理员点击员工风采信息管理功能,查看员工风采信息,点击员工风采信息添加功能,输入员工风采信息然后点击提交按钮就可以完成员工风采信息的添加。客户需求信息关系着客户的家政服务预约,管理员可以查询和修改客户需求信息,还可以查看客户需求的添加时间。接单信息属于本系统里的核心数据,管理员可以对接单的信息进行查询。本功能设计的目的可以使家政服务进行及时的安排。管理员可以查询员工信息,可以进行修改删除。 客户可以查看自己的预约和修改自己的资料并发布需求以及管理接单信息等。 在首页里可以看到管理员添加和管理的信息,客户可以在首页里进行家政服务的预约和公司介绍信息的了解。 员工可以查询客户需求进行接单以及管理家政服务信息和留言信息、收藏信息等。
LeetCode-Editor是一种在线编码工具,它提供了一个用户友好的界面编写和运行代码。在使用LeetCode-Editor时,有时候会出现乱码的问题。 乱码的原因可能是由于编码格式不兼容或者编码错误导致的。在这种情况下,我们可以尝试以下几种解决方法: 1. 检查文件编码格式:首先,我们可以检查所编辑的文件的编码格式。通常来说,常用的编码格式有UTF-8和ASCII等。我们可以将编码格式更改为正确的格式。在LeetCode-Editor中,可以通过界面设置或编辑器设置来更改编码格式。 2. 使用正确的字符集:如果乱码是由于使用了不同的字符集导致的,我们可以尝试更改使用正确的字符集。常见的字符集如Unicode或者UTF-8等。在LeetCode-Editor中,可以在编辑器中选择正确的字符集。 3. 使用合适的编辑器:有时候,乱码问题可能与LeetCode-Editor自身相关。我们可以尝试使用其他编码工具,如Text Editor、Sublime Text或者IDE,看是否能够解决乱码问题。 4. 查找特殊字符:如果乱码问题只出现在某些特殊字符上,我们可以尝试找到并替换这些字符。通过仔细检查代码,我们可以找到导致乱码的特定字符,并进行修正或替换。 总之,解决LeetCode-Editor乱码问题的方法有很多。根据具体情况,我们可以尝试更改文件编码格式、使用正确的字符集、更换编辑器或者查找并替换特殊字符等方法来解决这个问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值