LeetCode 树-简单题 4个典例

简介: LeetCode 树-简单题 4个典例

个人感觉学会这4个典例基本就算把初级树题刷完了(代码都参考了效率100%的)(主要是其中遍历手法)!So,Enjoy!

二叉树的所有路径(带父信息)

class Solution {
   
    public List<String> binaryTreePaths(TreeNode root) {
   
        List<String> res=new ArrayList<>(100);
        dfs(root,"",res);
        return res;
    }
    public void dfs(TreeNode root,String la,List<String> res){
   
        if (root==null){
   
            return;
        }
        StringBuilder sbu=new StringBuilder(la);
        sbu.append(root.val);
        if (root.left==null&&root.right==null){
   
            res.add(sbu.toString());
        }else{
   
            sbu.append("->");
            dfs(root.left,sbu.toString(),res);
            dfs(root.right,sbu.toString(),res);
        }
    }
}

树直径 (展开与收缩)

public class DiameterOfBinaryTree {
   
    int ans;
    public int diameterOfBinaryTree(TreeNode root) {
   
        ans = 1;
        depth(root);
        return ans - 1;
    }
    public int depth(TreeNode node) {
   
        if (node == null) {
   
            return 0; // 访问到空节点了,返回0
        }
        int L = depth(node.left); // 左儿子为根的子树的深度
        int R = depth(node.right); // 右儿子为根的子树的深度
        ans = Math.max(ans, L+R+1); // 计算d_node即L+R+1 并更新ans
        return Math.max(L, R) + 1; // 返回该节点为根的子树的深度
    }
}

二叉树的层平均值 (带层遍历)

class Solution {
   
    public int getH(TreeNode root) {
   
        if (root == null) {
   
            return 0;
        }
        int left = 1 + getH(root.left);
        int right = 1 + getH(root.right);
        return Math.max(left, right);
    }

    public List<Double> averageOfLevels(TreeNode root) {
   
        int h = getH(root);
        long[] sums = new long[h];
        int[] counts = new int[h];
        dfs(root, 0, sums, counts);
        List<Double> ret = new ArrayList<>(h);
        for (int i = 0; i < h; i++) {
   
            ret.add(1.0 * sums[i] / counts[i]);
        }
        return ret;
    }

    public void dfs(TreeNode root, int h, long[] sums, int[] counts) {
   
        if (root == null) {
   
            return;
        }
        sums[h] += root.val;
        counts[h] += 1;
        dfs(root.left, h + 1, sums, counts);
        dfs(root.right, h + 1, sums, counts);
    }

}

相同的树(两树同时遍历)

class Solution {
   
    public boolean isSameTree(TreeNode p, TreeNode q) {
   
        if (p==null&&q==null)return true;
        if (p==null||q==null){
   
            return false;
        }
        if (p.val!=q.val)return false;
        return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
    }
}
目录
相关文章
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 26. 树的子结构
这篇文章提供了解决LeetCode上"剑指Offer 26. 树的子结构"问题的Python代码实现和解析,判断一棵树B是否是另一棵树A的子结构。
35 4
|
1月前
|
Python
【Leetcode刷题Python】538. 把二叉搜索树转换为累加树
LeetCode上538号问题"把二叉搜索树转换为累加树"的Python实现,使用反向中序遍历并记录节点值之和来更新每个节点的新值。
17 3
|
4月前
|
算法 C语言 容器
从C语言到C++_25(树的十道OJ题)力扣:606+102+107+236+426+105+106+144+94+145(下)
从C语言到C++_25(树的十道OJ题)力扣:606+102+107+236+426+105+106+144+94+145
49 7
|
4月前
|
C语言
从C语言到C++_25(树的十道OJ题)力扣:606+102+107+236+426+105+106+144+94+145(中)
从C语言到C++_25(树的十道OJ题)力扣:606+102+107+236+426+105+106+144+94+145
41 1
|
4月前
|
算法 C语言 C++
从C语言到C++_25(树的十道OJ题)力扣:606+102+107+236+426+105+106+144+94+145(上)
从C语言到C++_25(树的十道OJ题)力扣:606+102+107+236+426+105+106+144+94+145
30 1
|
4月前
LeetCode———100——相同的树
LeetCode———100——相同的树
|
4月前
力扣337.打家劫舍3(树形dp)
力扣337.打家劫舍3(树形dp)
|
3月前
|
SQL 算法 数据可视化
LeetCode题目99:图解中叙遍历、Morris遍历实现恢复二叉树搜索树【python】
LeetCode题目99:图解中叙遍历、Morris遍历实现恢复二叉树搜索树【python】
|
3月前
|
存储 SQL 算法
LeetCode题目100:递归、迭代、dfs使用栈多种算法图解相同的树
LeetCode题目100:递归、迭代、dfs使用栈多种算法图解相同的树
|
3月前
|
存储 算法 数据可视化
python多种算法对比图解实现 验证二叉树搜索树【力扣98】
python多种算法对比图解实现 验证二叉树搜索树【力扣98】