zoukankan      html  css  js  c++  java
  • 404. 左叶子之和

    404. 左叶子之和

    题目链接:404. 左叶子之和(简单)

    计算给定二叉树的所有左叶子之和。

    示例:

        3
      / \
    9 20
      / \
      15   7

    在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24

    题解

    思路

    这道题的关键是如何找到左叶子节点。通过观察,我们可以发现,判断节点是否是左叶子节点需要其父节点的帮助,即当 父节点的左子节点不为空,左子节点的左右子节点都为空 时,这个左子节点就是左叶子节点。该题可以采取“前中后序遍历”,可以采用“递归或迭代方法”。

    递归(前序遍历)

    代码(C++)
    //递归法(前序遍历)
    class Solution {
    public:
        void sumLeft(TreeNode* node, int& result){
            if (node->left == nullptr && node->right == nullptr) return;
            if (node->left != nullptr && node->left->left == nullptr && node->left->right == nullptr) { //
                result += node->left->val;
            }
            if (node->left != nullptr) sumLeft(node->left, result); //
            if (node->right != nullptr) sumLeft(node->right, result); //
    ​
        }
        int sumOfLeftLeaves(TreeNode* root) {
            int result = 0;
            if (root == nullptr) return result;
            sumLeft(root, result);
            return result;
        }
    };
    代码(JavaScript)
    //递归
    var sumOfLeftLeaves = function(root) {
        if (root === null) return 0;
        var midValue = 0;
        if (root.left != null && root.left.left === null && root.left.right === null) {
            midValue = root.left.val;
        }
        var leftValue = sumOfLeftLeaves(root.left);
        var rightValue = sumOfLeftLeaves(root.right);
        var result = midValue + leftValue + rightValue;
        return result;
    };

    统一迭代(后序遍历)

    代码(C++)
    //统一迭代法(后序遍历)
    class Solution {
    public:
        int sumOfLeftLeaves(TreeNode* root) {
            int result = 0;
            stack<TreeNode*> sta;
            if(root != nullptr) sta.push(root);
            while (!sta.empty()) {
                TreeNode* node = sta.top();
                if (node != nullptr) {
                    sta.pop();
                    if (node->left) sta.push(node->left);
                    if (node->right) sta.push(node->right);
                    sta.push(node);
                    sta.push(nullptr);
                } else {
                    sta.pop();
                    node = sta.top();
                    sta.pop();
                    if (node->left != nullptr && node->left->left == nullptr && node->left->right == nullptr) {
                        result += node->left->val;
                    }
                }
            }
            return result;
        }
    };
    代码(JavaScript)
    //迭代
    var sumOfLeftLeaves = function(root) {
        var result = 0;
        let stackNode = [];
        if (root != null) stackNode.push(root);
        while (stackNode.length != 0) {
            var node = stackNode.pop();
            if (node != null) {
                if (node.left) stackNode.push(node.left);
                if (node.right) stackNode.push(node.right);
                stackNode.push(node);
                stackNode.push(null);
            } else {
                node = stackNode.pop();
                if (node.left != null && node.left.left === null && node.left.right === null) {
                    result += node.left.val;
                }
            }
        }
        return result;
    };

     

     

     

  • 相关阅读:
    【LGR-070】洛谷 3 月月赛-官方题解
    洛谷P1034 矩形覆盖
    <C和指针---读书笔记9>
    <C和指针---读书笔记8>
    <C和指针---读书笔记7>
    <C和指针---读书笔记6>
    <C和指针---读书笔记1>
    <C和指针---读书笔记2>
    <C和指针---读书笔记5>
    <C和指针---读书笔记4>
  • 原文地址:https://www.cnblogs.com/wltree/p/15665470.html
Copyright © 2011-2022 走看看