力扣559、222、938-java刷题笔记

本文介绍了如何使用迭代和递归方法解决LeetCode中的三个问题:N叉树的最大深度,完全二叉树的节点个数,以及二叉搜索树范围内节点值的和。详细展示了层序遍历、前序遍历在这些问题中的应用。
摘要由CSDN通过智能技术生成

一、559. N 叉树的最大深度 - 力扣(LeetCode)

1.1题目

给定一个 N 叉树,找到其最大深度。

最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。

N 叉树输入按层序遍历序列化表示,每组子节点由空值分隔(请参见示例)。

示例 1:

输入:root = [1,null,3,2,4,null,5,6]
输出:3

示例 2:

输入:root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
输出:5

1.2思路分析

方法一迭代:层序遍历

方法二递归

1.3代码实现

方法一迭代:层序遍历

/*
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

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

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};
*/
// 层序法处理
class Solution {
    public int maxDepth(Node root) {
        if(root == null) return 0;
        Queue <Node> queue = new LinkedList<>();
        queue.offer(root);
        int deep = 0;
        while(!queue.isEmpty()){
            int size = queue.size();
            while(size>0){
               Node temp = queue.poll();
               for(Node ch : temp.children)
               queue.offer(ch);
               size--;
            }
            deep++;
        }
        return deep;
    }
}

方法二递归

/*
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

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

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
    public int maxDepth(Node root) {
        if(root == null) return 0;
        int deep = 0;
        // 遍历root的孩子
        for(Node ch : root.children){
            deep = Math.max(deep,maxDepth(ch));
        }
        // 每执行一次深度加一
        return deep+1;
    }
}

二、222. 完全二叉树的节点个数 - 力扣(LeetCode)

2.1题目

给你一棵 完全二叉树 的根节点 root ,求出该树的节点个数。

完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h 个节点。

示例 1:

输入:root = [1,2,3,4,5,6]
输出:6

示例 2:

输入:root = []
输出:0

示例 3:

输入:root = [1]
输出:1

2.2思路分析

方法一递归:前序

方法二迭代:前序、层序

2.3代码实现

方法一递归

/**
 * 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 countNodes(TreeNode root) {
        if(root == null) return 0;
        
        return countNodes(root.left)+countNodes(root.right)+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 countNodes(TreeNode root) {
        if(root == null) return 0;
        int count = 0;
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode temp = stack.pop();
            count++;
            if(temp.right!=null) stack.push(temp.right);
            if(temp.left!= null) stack.push(temp.left);
        }
       return count;
    }
}
层序
/**
 * 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 countNodes(TreeNode root) {
    if(root == null) return 0;
    Queue <TreeNode> queue = new LinkedList();
    queue.offer(root);
    int count = 0;
    while(!queue.isEmpty()){
        int size = queue.size();
        while(size>0){
            TreeNode temp = queue.poll();
            count ++;
            if(temp.left!=null) queue.offer(temp.left);
            if(temp.right!=null) queue.offer(temp.right);
            size--;
        }
    
    }
    return count;
    }
}

三、938. 二叉搜索树的范围和 - 力扣(LeetCode)

3.1题目 

给定二叉搜索树的根结点 root,返回值位于范围 [low, high] 之间的所有结点的值的和。

示例 1:

输入:root = [10,5,15,3,7,null,18], low = 7, high = 15
输出:32

示例 2:

输入:root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10
输出:23

3.2思路分析

方法一递归

方法二迭代

3.3代码实现

方法一递归

/**
 * 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 rangeSumBST(TreeNode root, int low, int high) {
        if(root == null) return 0;
        int count = root.val;
        int ans = count >= low && count<= high ? count : 0;
        if(count >= low){
            ans += rangeSumBST(root.left,low,high);
        } 
        if(count<=high){
            ans += rangeSumBST(root.right,low,high);
        }

        return ans;
    }
}

方法二迭代

/**
 * 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 rangeSumBST(TreeNode root, int low, int high) {
        if(root == null) return 0;
        Stack <TreeNode> stack = new Stack<>();
        stack.push(root);
        int count = 0;
        while(!stack.isEmpty()){
            TreeNode temp = stack.pop();
            if(temp.val >=low && temp.val<= high) count += temp.val;
            if(temp.left!= null)  stack.push(temp.left);
            if(temp.right!=null) stack.push(temp.right);
        }
        return count;

    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值