美文网首页
算法记录 | Day15 二叉树(04)

算法记录 | Day15 二叉树(04)

作者: perry_Fan | 来源:发表于2022-11-10 15:41 被阅读0次
  • 110.平衡二叉树
    1. 二叉树的所有路径
  • 404.左叶子之和

【110.平衡二叉树 】

/**
 * 给定一个二叉树,判断它是否是高度平衡的二叉树。
 *
 * 本题中,一棵高度平衡二叉树定义为:一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。
 *
 * 示例 1:
 *
 * 给定二叉树 [3,9,20,null,null,15,7]
 */

public class balanceTree_110 {

    public boolean isBalanced(TreeNode root) {
        return getHeight(root) != -1;
    }

    private int getHeight(TreeNode root){
        if (root == null){
            return 0;
        }
        int leftHeight = getHeight(root.left);
        if(leftHeight == -1){
            return -1;
        }
        int rightHeight = getHeight(root.right);
        if (rightHeight == -1){
            return -1;
        }

        if (Math.abs(leftHeight - rightHeight) > 1){
            return -1;
        }

        return Math.max(leftHeight, rightHeight) + 1;
    }
}

【257. 二叉树的所有路径】

/**
 * 给你一个二叉树的根节点 root ,按 任意顺序 ,返回所有从根节点到叶子节点的路径。
 *
 * 叶子节点 是指没有子节点的节点。
 */
public class binaryTreePaths_257 {

    public List<String> binaryTreePaths(TreeNode root) {
        List<String> result = new ArrayList<>();
        if (root == null){
            return result;
        }

        Stack<Object> stack = new Stack<>();
        // 节点和路径同时入栈
        stack.push(root);
        stack.push(root.val + "");

        while(!stack.isEmpty()){
            String path = (String) stack.pop();
            TreeNode node = (TreeNode) stack.pop();

            // 若找到叶子节点
            if (node.left == null && node.right == null){
                result.add(path);
            }
            // 右子节点不为空
            if (node.right != null){
                stack.push(node.right);
                stack.push(path + "->" + node.right.val);
            }
            // 左子节点不为空
            if (node.left != null){
                stack.push(node.left);
                stack.push(path + "->" + node.left.val);
            }
        }
        return result;
    }
}

【404.左叶子之和 】

/**
 * 给定二叉树的根节点 root ,返回所有左叶子之和。
 */
public class sumOfLeftLeaves_404 {

    public int sumOfLeftLeaves(TreeNode root) {
        if (root == null) {
            return 0;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.add(root);
        int result = 0;
        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            if (node.left != null && node.left.left == null && node.left.right == null) {
                result += node.left.val;
            }
            if (node.right != null){
                stack.add(node.right);
            }
            if (node.left != null){
                stack.add(node.left);
            }
        }
        return result;
    }
}

相关文章

  • 二叉树遍历

    看了左程云老师的算法课,记录学习过程,整理思路和形成系统认识。 题目(算法课第五课) 二叉树遍历。二叉树定义,和二...

  • 二叉树遍历算法学习笔记

    前言 记录二叉树算法的学习过程,方便以后回顾。 二叉树的概念可查看 leetcode 二叉树遍历动态图,请查看 l...

  • 每日Leetcode—算法(10)

    100.相同的树 算法: 101.对称二叉树 算法: 104.二叉树的最大深度 算法: 107.二叉树的层次遍历 ...

  • 每日Leetcode—算法(11)

    110.平衡二叉树 算法: 111.二叉树的最小树深 算法: 112.路径总和 算法:

  • Algorithm小白入门 -- 二叉树

    二叉树二叉树构造二叉树寻找重复子树 1. 二叉树 基本二叉树节点如下: 很多经典算法,比如回溯、动态规划、分治算法...

  • 二叉树的基本算法

    二叉树的基本算法 树、二叉树 的基本概念,参考数据结构算法之美-23讲二叉树基础(上):树、二叉树[https:/...

  • 面试题

    近期算法题记录 始于2021.08.05 一、字节跳动 算法题:二叉树,根结点到每个叶子结点的值算一个数,所有的值...

  • Javascript实现二叉树算法

    最近正在看慕课网的《Javascript实现二叉树算法》课程,现把看到的东西记录下来。什么是二叉树?简单来说,二叉...

  • LeetCode刷题-我会翻转二叉树,谷歌还要我吗?

    前言说明 算法学习,日常刷题记录。 题目连接 翻转二叉树[https://leetcode-cn.com/prob...

  • 二叉树的中序遍历(Java)——Morris迭代算法

    二叉树的中序遍历 对于此题而言,若采用递归算法(简单),我们使用深度优先算法来遍历二叉树。若采用迭代算法,我们使用...

网友评论

      本文标题:算法记录 | Day15 二叉树(04)

      本文链接:https://www.haomeiwen.com/subject/rrwztdtx.html