zoukankan      html  css  js  c++  java
  • Leetcode 404 左叶子之和 BFS与DFS

      题目简单,练习一下 BFS 与 DFS。

      BFS:

        private final boolean isLeaf(TreeNode node) {
            return node.left == null && node.right == null;
        }
    
        public final int sumOfLeftLeavesBFS(TreeNode root) {
            if (root == null) return 0;
            Queue<TreeNode> queue = new LinkedList<TreeNode>();
            queue.offer(root);
            int res = 0;
            while (!queue.isEmpty()) {
                TreeNode node = queue.poll();
                if (node.left != null) {
                    if (isLeaf(node.left)) res += node.left.val;
                    else queue.offer(node.left);
                }
                if (node.right != null && !isLeaf(node.right)) queue.offer(node.right);
            }
            return res;
        }

      DFS:

        public final int sumOfLeftLeaves(TreeNode root) {
            return sumOfLeftLeavesDFS(root);
        }
    
        public final int sumOfLeftLeavesDFS(TreeNode root) {
            if (root == null) return 0;
            int res = 0;
            if (root.left != null) {
                if (isLeaf(root.left)) res += root.left.val;
                else res += sumOfLeftLeavesDFS(root.left);
            }
            if (root.right != null && !isLeaf(root.right)) res += sumOfLeftLeavesDFS(root.right);
            return res;
        }
    
        private final boolean isLeaf(TreeNode node) {
            return node.left == null && node.right == null;
        }

      JS BFS:

    var isLeaf = function (node) {
        return node.left == null && node.right == null;
    }
    
    
    var sumOfLeftLeaves = function (root) {
        if (root == null) return 0;
        let queue = [];
        queue.push(root);
        let res = 0;
        while (queue.length > 0) {
            let node = queue.shift();
            if (node.left != null) {
                if (isLeaf(node.left)) res += node.left.val;
                else queue.push(node.left);
            }
            if (node.right != null && !isLeaf(node.right)) queue.push(node.right);
        }
        return res;
    }

      JS DFS:

    var sumOfLeftLeaves = function (root) {
        if (root == null) return 0;
        return dfs(root);
    };
    
    var dfs = function (node) {
        let res = 0;
        if (node.left != null) {
            if (isLeaf(node.left)) res += node.left.val;
            else res += dfs(node.left);
        }
        if (node.right != null && !isLeaf(node.right)) res += dfs(node.right);
        return res;
    }
    
    var isLeaf = function (node) {
        return node.left == null && node.right == null;
    }
  • 相关阅读:
    mingw-gcc-10.0.1-experimental-i686-posix-sjlj-20200202-0303907
    可以修改 QtScrcpy 窗口大小的小工具
    autosub 添加代理服务器参数 -P --proxy
    Python网络数据采集系列-------概述
    【刷题笔记】I'm stuck! (迷宫)-----java方案
    【刷题笔记】火车购票-----java方案
    mvc自定义全局异常处理
    使用html2canvas实现浏览器截图
    再谈Newtonsoft.Json高级用法
    Spire.Doc组件读取与写入Word
  • 原文地址:https://www.cnblogs.com/niuyourou/p/13727585.html
Copyright © 2011-2022 走看看